您的位置:首页 > 其它

1040. Longest Symmetric String

2015-12-02 14:00 267 查看


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<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
freopen("F://Temp/input.txt", "r", stdin);
string input;
string input_reverse;
getline(cin, input);
input_reverse = input;
reverse(input_reverse.begin(), input_reverse.end());

int same = 0, sum = 0;
for(int i = 0; i < input.size(); i ++)
{
sum = 0;
for(int j = 0, j_rev = i; j < input.size()-i; j ++, j_rev ++)
{
if(input_reverse[j_rev] == input[j])
{
sum ++;
if(sum > same)
same = sum;
}
else
sum = 0;
}
sum = 0;
for(int j = 0, j_rev = i; j < input.size()-i; j ++, j_rev ++)
{
if(input_reverse[j] == input[j_rev])
{
sum ++;
if(sum > same)
same = sum;
}
else
sum = 0;
}
}
cout<<same<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: