PAT-1040

1040. Longest Symmetric String

题目地址:http://pat.zju.edu.cn/contests/pat-practise/1040

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
 
 
#if 1

#include <stdio.h>
#include <string.h>

#define  MAXN  (1000+1)

int MaxOddSym(char s[],int axis)
{
	int j;
	int CurCnt;
	for(CurCnt = 0,j = 0;j <= axis;j++)
		{
			if(s[axis-j] != s[axis+j])
				break;
			else	
				CurCnt++;
		}
	return CurCnt*2-1;
}

int MaxEvenSym(char s[],int axis)
{
	int j;
	int CurCnt;
	for(CurCnt = 0,j = 0;j <= axis;j++)
		{
			if(s[axis-j] != s[axis+j+1])
				break;
			else	
				CurCnt++;
		}
	return CurCnt*2;
}

int FindLongestSymStr(char s[])
{
	int max,len,CurMax; 
	int i,j;
	len = strlen(s);
	
	for(max = 0,i = 0;i < len-(max)/2;i++)
	{
		CurMax = MaxOddSym(s,i) > MaxEvenSym(s,i) ? MaxOddSym(s,i) : MaxEvenSym(s,i);
		if(CurMax > max)
			max = CurMax;
	}
	return max;
}

char str[MAXN];
int main(int argc,char *argv[])
{
	int MaxSymStr;

	//freopen( "D:\\Chengsq\\in.txt" , "r" ,stdin);
   // freopen( "D:\\Chengsq\\out.txt", "w" ,stdout);

	while(gets(str))
	{
		getchar();
		MaxSymStr = FindLongestSymStr(str);
		printf("%d\n",MaxSymStr);
	}
	return 0;
}


#endif

你可能感兴趣的:(PAT-1040)