7-250 最长对称子串 (25 分)

7-250 最长对称子串 (25 分)

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

输入格式:

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

输出格式:

在一行中输出最长对称子串的长度。

输入样例:

Is PAT&TAP symmetric?

结尾无空行

输出样例:

11

结尾无空行

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	string s;
	getline(cin,s);//这个是整行读入 cin是读入到第一个空格处
	int x=1;
	for(int i=0; i<s.length(); i++) {
		for(int j=s.length()-1; j>=i; j--) {
			int left=i,right=j;
			while(left<=right&&s[left++]==s[right--]) {
				if(left>right)
					x=max(x,j-i+1);
			}
		}
	}
	cout<<x<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51916951/article/details/121085742