您的位置:首页 > 编程语言 > C语言/C++

【C++】PAT(advanced level)1040. Longest Symmetric String (25)

2014-03-12 10:23 543 查看

1040. Longest Symmetric String (25)

时间限制
400 ms

内存限制
32000 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


1.分情况讨论,题目介绍了奇数的情况,还要考虑偶数的情况。

2,如果没有对称,输出1.

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
#include<iomanip>
using namespace std;

int main(){
//freopen("in.txt","r",stdin);
char a[1001];
//	int m;
gets(a);
int i=0,sumMax=0,final=1;
//cout<<sizeof(a);
while(a[i+2]!='\0'){
if(a[i]==a[i+2]){
sumMax=3;
int j=1;
while(a[i+2+j]!='\0'&&a[i-j]==a[i+2+j]){
sumMax+=2;
j++;
}
}
i++;
if(sumMax>final){
final=sumMax;
}
}
i=0;
while(a[i+1]!='\0'){
if(a[i]==a[i+1]){
sumMax=2;
int j=1;
while(a[i+1+j]!='\0'&&a[i-j]==a[i+1+j]){
sumMax+=2;
j++;
}
}
i++;
if(sumMax>final){
final=sumMax;
}
}
cout<<final;
//	fclose(stdin);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: