当前位置:网站首页>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))
边栏推荐
- Ceres库运行,模板内报内存冲突问题。(已解决)
- 【解决方案 三十一】Navicat数据库结构同步
- 密码设置有关方法:不能相同字母,不能为连续字符
- 21天学习挑战赛--第二天打卡(setSystemUiVisibility、导航栏、状态栏)
- 面试官:如何查看/etc目录下包含abc字符串的文件?
- 微信小程序使用腾讯云对象储存上传图片
- [UML] Summary of Information System Analysis and Design Knowledge Points
- nVisual二次开发——第二章 nVisual API操作指南Swagger使用
- 【WeChat Mini Program】Social Internship Production Project for Information Management and Information System Major--Trash Fingerprint
- PMP每日一练 | 考试不迷路-8.4(包含敏捷+多选)
猜你喜欢
干货丨数学规划视角下的分货优化解题思路
rpm安装提示error: XXX: not an rpm package (or package manifest):
做项目管理有且有必要了解并学习的重要知识--PMP项目管理
“蔚来杯“2022牛客暑期多校训练营4 N
【WeChat Mini Program】Social Internship Production Project for Information Management and Information System Major--Trash Fingerprint
新 Nsight Graph、Nsight Aftermath 版本中的性能提升和增强功能
代码越写越乱?那是因为你没用责任链!
ShanDong Multi-University Training #4 A、B、C、G
一文梳理NLP主要模型发展脉络
技术分享| 小程序实现音视频通话
随机推荐
DateTimeFormatter api
RK1126编译gdb 板子上gdb调试程序
【PHP实现微信公众平台开发—基础篇】第1章 课程介绍
"Social Enterprises Conducting Civilian Personnel Training Specifications" group standard on the shelves of Xinhua Bookstore
JSX use
项目里的各种配置,你都了解吗?
为什么密码云服务平台是云时代的必然之选?
持续交付(二)PipeLine基本使用
Django框架MySQL数据库到models模型的映射关系
A discussion of integrated circuits
微信小程序使用腾讯云对象储存上传图片
RobotFramework二次开发(一)
Is the code more messy?That's because you don't use Chain of Responsibility!
【Game Of AutoTest】1、再度启程,重识游戏自动化测试
21天学习挑战赛--第二天打卡(setSystemUiVisibility、导航栏、状态栏)
HDU1580 输出先手能取的方案数
一分钟认识 IndexedDB 数据库,太强大了!
正确使用Impala的invalidate metadata与refresh语句
【自动微分实现】反向OO实现自动微分(Pytroch核心机制)
【PHP实现微信公众平台开发—基础篇】第2章 微信公众账号及申请流程详解