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 思路:直接遍历所有子串,再判断是否是对称字符串,取最长对称子串#include <string> #include <iostream> #include <algorithm> using namespace std; int main() { string str, st; getline(cin, str); int ans = 0; for (int i=0; i<=str.size()-1; ++i) for (int j=1; j<=str.size()-i; ++j) { //遍历所有子串 st = str.substr(i, j); string s1 = st; reverse(st.begin(), st.end()); //检查是否对称 if (st == s1 && st.size() > ans) ans = st.size(); } cout << ans << endl; //system("pause"); return 0; }