51nod 2级算法题-1092
2017-04-25 11:48
211 查看
1092 回文字符串
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。每个字符串都可以通过向中间添加一些字符,使之变为回文字符串。 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba。方案1只需要添加2个字符,是所有方案中添加字符数量最少的。
Input
输入一个字符串Str,Str的长度 <= 1000。
Output
输出最少添加多少个字符可以使之变为回文字串。
Input示例
abbc
Output示例
2
逆置字符串,求最大公共子序列。
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <queue> #include <stack> using namespace std; #define endl "\n" const int maxn = 1000+10; const int Mod=1000000007; int dp[maxn][maxn]; char Str[maxn]; char S[maxn]; int main (){ std::ios::sync_with_stdio(false); memset(dp,0,sizeof(dp)); cin>>Str; int len=strlen(Str); for(int i=0,j=len-1;i<len;i++,j--){ S[i]=Str[j]; } for(int i=1;i<=len;i++){ for(int j=1;j<=len;j++){ if(Str[i-1]==S[j-1]){ dp[i][j]=dp[i-1][j-1]+1; }else{ dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } } cout<<len-dp[len][len]<<endl; return 0; }
相关文章推荐
- 51nod 2级算法题-1007
- 51nod 2级算法题-1428
- 51nod 2级算法题-1010
- 【贪心+优先队列】1428 活动安排问题【51nod】【难度:2级算法题】
- 51nod 2级算法题-1014
- 51nod 2级算法题-1050
- 51nod 2级算法题-1062
- 【模拟】1432 独木舟【51nod】【难度:2级算法题】
- 51nod 2级算法题-1126
- 51nod 2级算法题-1133
- 51nod 2级算法题-1095
- 51nod 2级算法题-1279
- 51nod 2级算法题-1119
- 51nod 2级算法题-1315
- 51nod -- 1002 数塔取数问题(一级算法题)
- 51nod --1004 n^n的末位数字(一级算法题)
- 51nod-1092 回文字符串
- 51nod 算法马拉松6(索函数)(规律题目)
- 51nod 算法马拉松3 A:序列分解
- 51Nod-1785-数据流中的算法