题目
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
AC代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
getline(cin,str);
int maxsize=0;
for(int i=0;i<str.size();i++)
{
for(int j=1;j<=str.size()-i;j++)
{
string s1=str.substr(i,j),s2=s1;
reverse(s2.begin(),s2.end());
if(s1==s2&&s1.size()>maxsize) maxsize=s1.size();
}
}
cout<<maxsize;
}
参考
1040 Longest Symmetric String (25 分)(字符串处理)
知识点总结
这道题需要掌握substr的用法,详情可参考这篇文章——【C++】C++中substr的用法