当前位置:网站首页>PAT甲级:1040 Longest Symmetric String
PAT甲级:1040 Longest Symmetric String
2022-08-04 13:06:00 【正在黑化的KS】
题目描述:
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given
Is PAT&TAP symmetric?, the longest symmetric sub-string iss PAT&TAP s, hence you must output11.Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?Sample Output:
11代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题目大意:
给定一个字符串 求最长回文子串的长度
解题思路:
最长回文子串
dp[i][j] 表示 s[i] 至 s[j]如果是回文子串则为1, 反之则为0
1. s[i]==s[j] 那么只需要 s[i+1], s[j-1]是回文子串, s[i]至s[j] 就是回文子串: dp[i][j] = dp[i+1][j-1]2. s[i]!=s[j]:
dp[i][j] = 0 s[i]至s[j]不是回文子串
根据从边界出发的原理, 注意到边界都是长度为1或2的子串, 每次转移都对子串的长度-1, dp[i][j] = dp[i+1][j-1]不妨考虑按子串的长度和子串的初始位置进行枚举
Python3代码:
s = input()
length = len(s)
dp = [[0]*(length+10) for i in range(length+10)]
res = ''
for l in range(1,length+1) :
i = 0
while i + l - 1 < length :
j = i + l - 1
if l == 1 : dp[i][j] = 1
elif l == 2 and s[i] == s[j] : dp[i][j] = 2
else :
if s[i] == s[j] and dp[i+1][j-1] : dp[i][j] = dp[i+1][j-1] + 2
if dp[i][j] > len(res) : res = s[i:j+1]
i += 1
print(len(res))边栏推荐
- redisTemplate存取List遇到的坑
- Chinese valentine's day of young people crazy to make money, earn 140000 a week
- Haproxy搭建web群集
- 5 cloud security management strategies enterprises should implement
- LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
- MATLAB——图像分块
- RobotFramework二次开发(一)
- router---Route guard
- 【WeChat Mini Program】Social Internship Production Project for Information Management and Information System Major--Trash Fingerprint
- Arduino框架下I2S控制ADC采样以及PWM输出示例解析
猜你喜欢

Access Huawei game anti-addiction, click the anti-addiction pop-up window, the game crashes

Why don't young people like to buy Mengniu and Yili?

MySQL性能指标TPS\QPS\IOPS如何压测?

手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果

《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架

03 多线程与高并发 - ReentrantLock 源码解析

面试官:如何查看/etc目录下包含abc字符串的文件?

"Lonely Walking on the Moon" is a powerful medicine, it can't cure the internal friction of happy twist

Diffusion Models:生成扩散模型

Niuke.com Brush Question Record || Linked List
随机推荐
[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)
基于双层共识控制的直流微电网优化调度(Matlab代码实现)
nVisual二次开发——第二章 nVisual API操作指南Swagger使用
yum 查看已经安装过的包并卸载
Geoffrey Hinton:深度学习的下一个大事件
Script to get local IP address
《会面》-凯瑟琳曼斯菲尔德(徐志摩译)
MATLAB——图像分块
router---dynamic route matching
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
内存定位利器-ASAN使用小结
RK1126编译gdb 板子上gdb调试程序
sqlserver删除重复数据
密码设置十准则
做项目管理有且有必要了解并学习的重要知识--PMP项目管理
Various problems with npm install
Ultra-QuickSort
Cows 树状数组
sqlplus报错ORA-12547: TNS:lost contact解决
oracle sql中根据条件判断是否插入数据