题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1040
思路,按长度递减,依次判断子串即可!
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 bool Is_symmetric(string &s, int i, int j) 6 { 7 while(i<=j) 8 { 9 if(s[i] != s[j]) 10 return false; 11 ++i; 12 --j; 13 } 14 return true; 15 } 16 17 int main() 18 { 19 string input; 20 while(getline(cin, input)) 21 { 22 int length=input.length(); 23 bool flag(false); 24 for(int l=length; l != 0; --l) 25 if(flag == false) 26 for(int i=0; i != length-l+1; ++i) 27 { 28 if(Is_symmetric(input, i, i+l-1)) 29 { 30 cout<<l<<endl; 31 flag=true; 32 break; 33 } 34 } 35 else 36 break; 37 } 38 return 0; 39 }