//1040
//1040. Longest Symmetric String (25)
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
char str[1001];
gets(str);
int len = strlen(str);
int i = 0;
char center;
int j;
int k;
int count = 0;
bool flag = true;
int left = -1;
int right = -1;
int preCount = 0;
while( i< len)
{
center = str[i];
count = 1;
//找到周围与其相等的字符
for (j = i - 1;j>=0;j--)
{
left = j;
if (center != str[j])
{
left = j;
break;
}
count++;
}
if (j< 0)
{
left = -1;
}
for (k = i +1;k<len;k++)
{
right = k;
if (center != str[k])
{
break;
}
count ++;
}
if (k == len)
{
right = len;
}
//j 和 k不同了。。。
for (j = left,k = right ;j >=0 && k<len ;j--,k++)
{
if( str[j] == str[k] )
{
count+= 2;
}
if( str[j] != str[k] )
{
break;
}
}
if(preCount < count)
preCount = count;
i++;
}
cout << preCount;
return 0;
}
PAT 1040. Longest Symmetric String (25)
最新推荐文章于 2019-08-29 09:01:22 发布