Pat(Advanced Level)Practice--1040(Longest Symmetric String)

Pat1040代码

题目描述:

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<cstdio>
#include<cstring>
#define MAX 1005

using namespace std;

int main(int argc,char *argv[])
{
	char s[MAX];
	int i,j,k;
	int max=0;
	int len,flag;
	fgets(s,sizeof(s),stdin);
	len=strlen(s);
	for(i=0;i<len;i++)
		for(j=i;j<len;j++)
		{
			flag=0;
			for(k=i;k<=j;k++)
			{
				if(s[k]!=s[i+j-k])
				{
					flag=1;
					break;
				}
			}
			if(!flag&&j-i+1>max)
				max=j-i+1;
		}
	printf("%d\n",max);

	return 0;
}


你可能感兴趣的:(C++,pat,基础题,advance)