Description
Given a string S, which consists of lowercase characters, you need to find the longest palindromic sub-string.
A sub-string of a string S is another string S’ that occurs “in” S. For example, “abst” is a sub-string of “abaabsta”. A palindrome is a sequence of characters which reads the same backward as forward.
Input
There are several test cases.
Each test case consists of one line with a single string S (1 ≤ |S | ≤ 50).
Output
For each test case, output the length of the longest palindromic sub-string.
Sample Input
sasadasa
bxabx
zhuyuan
Sample Output
7
1
3
题意:求字符串中最长的回文串中的长度,例如sasadasa
中asadasa 是最长的回文串,,
#include<bits/stdc++.h>
using namespace std;
string expand_palindrome(string s, int l, int r) {
int n = s.length();
while(l >= 0 && r < n && s[l] == s[r]) {
l--;
r++;
}
return s.substr(l + 1, r - l - 1);
}
//函数解释详见下面链接
string get_longest_palin(string s) {
int i;
int n = s.length();
string longest = s.substr(0, 1);
string t;
for(i = 0; i < n; i++) {
t = expand_palindrome(s, i, i);
if(t.length() > longest.length()) {
longest = t;
}
t = expand_palindrome(s, i, i + 1);
if(t.length() > longest.length()) {
longest = t;
}
}
return longest;
}
int main()
{
string s;
while(cin>>s)
{
cout<<get_longest_palin(s).size()<<endl;
}
return 0;
}