#pragma warning(disable:4996)
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;
int dp[1010][1010];
int strlen(char s[])
{
int i = 0;
while (s[i] != '\0')
i++;
return i;
}
int main()
{
char s[1010];
int n, m, l;
scanf("%[^\n]", s + 1);
int len = strlen(s+ 1) ;
for (int i = 0; i < 1010; i++)
for (int j = 0; j < 1010; j++)
dp[i][j] = 0;
int ans = 1;
for (int i = 0; i < len; i++)
{
dp[i][i] = 1;
if (s[i] == s[i + 1])
{
dp[i][i + 1] = 1;
ans = 2;
}
}
dp[len][len] = 1;
for (int i = 2; i <= len - 1; i++)
{
for (int j = 1; j + i <= len; j++)
{
if ((s[j] == s[j + i]) && dp[j + 1][j + i - 1] == 1)
{
ans = i + 1;
dp[j][j + i] = 1;
}
}
}
printf("%d", ans);
}
PAT1040 Longest Symmetric String (25分) 最长回文子串
最新推荐文章于 2020-11-21 15:40:54 发布