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

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
#include
#include
#include
#include
#include
#include
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<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<


你可能感兴趣的:(C++/C)