python做网站guthub友情链接可以随便找链接加吗
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?
,最长对称子串为s PAT&TAP s
,于是你应该输出11。
输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
思路:回文字符串正反相等,反转字符串找最长公共字串即可
#include "bits/stdc++.h"
using namespace std;
int f[1000 + 10][1000 + 10]={0};
int main(){string a, b;getline(cin, a);b = a;reverse(b.begin(), b.end());int n = a.length();for(int i = 0; i < a.length(); i++){for(int j =0; j < a.length(); j++){if(a[i] == b[j]){f[i+1 ][j+1 ] = f[i ][j ] + 1; }else {f[i+1][j+1] = 0;}}}int maxx = 0;for(int i = 0; i < 1010; i ++){for(int j = 0; j < 1010; j ++){maxx = max(f[i][j], maxx);} }cout<<maxx<<endl;return 0;
}