首页 > 学院 > 开发设计 > 正文

最长对称子串 -- 天梯模拟

2019-11-08 00:52:53
字体:
来源:转载
供稿:网友

Think: 判断字符串中最大的回文串长度,长度较小,直接暴力就可以过。。。 是PTA的模拟题。。。

题目 对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。 输入格式:

输入在一行中给出长度不超过1000的非空字符串。 输出格式:

在一行中输出最长对称子串的长度。 输入样例:

Is PAT&TAP symmetric?

输出样例:

11

各个测试点的坑 测试点1 答案正确 sample等价 测试点2 答案正确 有2段对称,后段长 测试点3 答案正确 偶数对称 测试点4 答案正确 最大N全对称 测试点5 答案正确 全不对称 测试点6 答案正确 最短串 测试点7 答案正确 最长串的局部不对称较长

#include<bits/stdc++.h>#include<string>using namespace std;bool judge(string & str, int s, int len);int main() { string str; int i, j; getline(cin,str); int Max = 1; for (i = 0;i <= str.size() - 1;i ++) { for (j = 1;j <= str.size() - i;j ++) { if (judge(str,i,j) && j > Max) Max = j; } } cout << Max << endl; return 0; } bool judge(string & str, int s, int len) { int i; for (i = s;i < len / 2 + s;i ++) { if (str[i] != str[s + len - i + s - 1]) return false; } return true; }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表