PTA L2-008 最长对称子串 (25 分)
2020-02-01 01:25
281 查看
题目描述:
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定
Is PAT&TAP symmetric?,最长对称子串为
s PAT&TAP s,于是你应该输出11。
输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
[code]Is PAT&TAP symmetric?
输出样例:
[code]11
解题思路:
主要就是扫描字符串,具体思路看代码把,比较好理解
AC代码:
[code] #include<stdio.h> #include<string.h> int main(int argc, char *argv[]) { char x[1009]; gets(x); int length=strlen(x); int max=1; for(int n=0;n<length;n++) { int m=length-1; while(m>n) { if(x[m]==x ) { int t=n; int t1=m; int l=0; while(t<t1) { if(x[t]==x[t1]) { l=l+2; } else { break; } t++; t1--; } if(t>t1&&x[t]==x[t1]) { if(l>max) { max=l; } } else if(t==t1) { l++; if(l>max) { max=l; } } } m--; } } printf("%d\n",max); return 0; }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- PTA-L2-008 最长对称子串 (25 分)
- L2-008 最长对称子串 (25 分)
- PAT L2-008. 最长对称子串(25) (暴力,Manacher算法和DP解决)
- PTA L2-008 最长对称子串
- L2-008. 最长对称子串----暴力+dp+Manacher
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008 最长对称子串
- 天梯赛L2-008最长对称子串
- PAT ~ L2-008. 最长对称子串 (Manacher)
- L2-008. 最长对称子串
- pat L2-008. 最长对称子串
- 团体程序设计天梯赛-练习集 L2-008. 最长对称子串
- 团体程序设计天梯赛-练习集L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串