标签:return using main sam 遍历 cin put 输入输出 string
找出字符串中最长的回文子串长度
Sample Input:
Is PAT&TAP symmetric?
Sample Output:Sample Output:
11
dp的思想,设字符串str,dp[i][j] = 1 or 0 代表str[i] ~ str[j]间是否是一个回文串
有:
#include <iostream>
using namespace std;
int dp[1010][1010];
int main() {
string s;
getline(cin, s);
int len = s.length(), ans = 1;
for(int i = 0; i < len; i++)
{
dp[i][i] = 1;
if(i < len - 1 && s[i] == s[i + 1])
{
dp[i][i + 1] = 1;
ans = 2;
}
}
for(int i = 2; i < len; i++)
{
for(int j = 0; j < len - i; j++)
{
if(dp[j + 1][j + i - 1] == 1 && s[j] == s[j + i])
{
dp[j][j + i] = 1;
ans = i + 1;
}
}
}
printf("%d", ans);
return 0;
}
PAT-甲级-1040 Longest Symmetric String (dp(二维))
标签:return using main sam 遍历 cin put 输入输出 string
原文地址:https://www.cnblogs.com/liushz-blog/p/14587886.html