您的位置:首页 > 其它

PAT 1040. Longest Symmetric String (25)

2015-08-17 16:47 519 查看


1040. Longest Symmetric String (25)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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 <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>
using namespace std;

int main(void)
{
string s;
getline(cin,s);
int size=s.size(),max=0;
if(size==1)
{
cout<<1;
return 0;
}
for(int i=0;i<size;i++)
{
int j=1,count=1;
while(i-j>=0&&i+j<size)
{
if(s[i+j]==s[i-j])
{
count+=2;
j++;
}
else
break;
}
if(count>max)
max=count;
j=0;
count=0;
while(i-j>=0&&i+j+1<size)
{
if(s[i-j]==s[i+j+1])
{
count+=2;
j++;
}
else
break;
}
if(count>max)
max=count;
}
cout<<max;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: