1040 Longest Symmetric String (25 分)(字串處理)
阿新 • • 發佈:2018-12-15
1040 Longest Symmetric String (25 分)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
本題大意
輸入一個字串,求該字串中最長對稱子串的長度。
分析
本題考查字串處理的知識,掌握string的一些常用函式(e.g. substr,獲取字串子串)解此題不難。
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
string s;
int maxlen=0;
getline(cin,s);
for(int i=0;i<s.size();i++){
for(int j=i+1;j<=s.size();j++){
string s1=s.substr(i,j),s2=s1;
reverse(s2.begin(),s2.end());
if(s2==s1 && maxlen<s1.size()) maxlen=s1.size ();
}
}
printf("%d",maxlen);
return 0;
}