L2-008 最长对称子串
2019-03-27 08:12
176 查看
题解
- vis[i][j]表示[i,j]是否为对称子串,dp[i][j]表示[i,j]最大的对称子串的长度
- if (s[i] == s[j] && vis[i+1][j-1]) dp[i][j] = dp[i+1][j-1] + 2,vis[i][j] = true;
- else dp[i][j] = max(max(dp[i+1][j],dp[i][j-1]),dp[i+1][j-1]);
题意
[code]#include <bits/stdc++.h> using namespace std; int const N = 1000 + 10; int dp ,vis ; char s ; int main() { cin.getline(s,N); int len = strlen(s); for(int i=0;i<len;i++){ dp[i][i] = 1; vis[i][i] = true; if(i != len - 1 && s[i+1] == s[i]){ dp[i][i+1] = 2; vis[i][i+1] = true; } } for(int k=3;k<=len;k++) for(int i=0;i+k-1<len;i++){ int j = i + k - 1; if(s[i] == s[j] && vis[i+1][j-1] == true){ vis[i][j] = true; dp[i][j] = dp[i+1][j-1] + 2; }else dp[i][j] = max(max(dp[i+1][j],dp[i][j-1]),dp[i+1][j-1]); } cout<<dp[0][len-1]<<endl; return 0; }
相关文章推荐
- L2-008. 最长对称子串
- L2-008. 最长对称子串(manacher算法)
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- PAT ~ L2-008. 最长对称子串 (Manacher)
- PAT 天梯赛 L2-008. 最长对称子串 【字符串】
- L2-008. 最长对称子串
- L2-008 最长对称子串
- L2-008. 最长对称子串
- 天梯赛L2-008最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- pat L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串
- 团体程序设计天梯赛-练习集 L2-008. 最长对称子串
- 天梯赛习题 L2-008. 最长对称子串
- L2-008. 最长对称子串
- L2-008. 最长对称子串 - 天梯赛练习题
- L2-008. 最长对称子串