PTA-L2-008 最长对称子串 (25 分)
2019-03-28 09:16
190 查看
L2-008 最长对称子串 (25 分)
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。
输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
#include <iostream> #include <cstring> using namespace std; char a[1010]; int is(int i, int j) //判断回文,返回值为长度 { int len = j-i+1; while(i <= j) { if(a[i++] != a[j--]) return 0; } return len; } int main() { int len, tmp, i, j, ans = 1; cin.getline(a, 1010); len = strlen(a); for(i = 0;i < len-1;i++) //遍历字符串 { for(j = i+1;j < len;j++) { if(a[i] == a[j]) //若发现相同的元素 { tmp = is(i,j); //判断回文,比较长度 ans = max(ans, tmp); } } } cout<<ans<<endl; return 0; }
相关文章推荐
- PTA L2-008 最长对称子串
- PAT L2-008. 最长对称子串(25) (暴力,Manacher算法和DP解决)
- L2-008 最长对称子串 (25 分)
- L2-008. 最长对称子串
- L2-008. 最长对称子串----暴力+dp+Manacher
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- PAT L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串-PAT团体程序设计天梯赛GPLT
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- 天梯赛习题 L2-008. 最长对称子串
- PAT L2-008. 最长对称子串
- L2-008. 最长对称子串
- PAT-L2-008(最长对称子串)
- L2-008. 最长对称子串
- PAT L2-008. 最长对称子串
- L2-008. 最长对称子串