1040. Longest Symmetric String (25)

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	char s[1010];
	gets(s);

	int len = strlen(s), i, count, res = 0, j, k;
	for(i = 0; i <= len - 1; i++)
	{
		count = 1;
		for(j = i - 1, k = i + 1; j >= 0 && k <= len - 1; j--, k++)
		{
			if(s[j] != s[k])
			{
				break;
			}

			count += 2;
		}

		if(count > res)
		{
			res = count;
		}

		count = 0;
		for(j = i, k = i + 1; j >= 0 && k <= len - 1; j--, k++)
		{
			if(s[j] != s[k])
			{
				break;
			}

			count += 2;
		}

		if(count > res)
		{
			res = count;
		}
	}

	printf("%d\n", res);

	system("pause");
	return 0;
}

 

posted on 2025-11-23 17:04  王景迁  阅读(0)  评论(0)    收藏  举报

导航