【POJ1159】【DP】17.2.6 T1 强迫症 题解
2017-02-06 21:08
309 查看
题目描述
回文串就是正反着看都一样的串(如abba,acbca)。jyb 不幸得了回文串强迫症,看到一个串就想把它变成回文串。jyb 可以在串中任意位置插入字符。例如Ab3bd,插入2 个字符可以变dAb3bAd 或者 Adb3bdA。现在jyb 想知道,最少插入多少个字符,才能把一个串变成回文串呢?
【输入格式】
一个包含大小写字母和数字的字符串s,区分大小写。
【输出格式】
一个整数,最少插入的字符数量。
【输入样例】
Ab3bd
【输出样例】
2
【数据规模】
20% 数据满足 s 的长度 ≤10。
100%数据满足s 的长度 ≤5000。
题目类似POJ1159(几乎一样)
此题直接正反相互求一遍LCS就完了,水题。(虽然没有一遍A,zz…)
附AC代码:
回文串就是正反着看都一样的串(如abba,acbca)。jyb 不幸得了回文串强迫症,看到一个串就想把它变成回文串。jyb 可以在串中任意位置插入字符。例如Ab3bd,插入2 个字符可以变dAb3bAd 或者 Adb3bdA。现在jyb 想知道,最少插入多少个字符,才能把一个串变成回文串呢?
【输入格式】
一个包含大小写字母和数字的字符串s,区分大小写。
【输出格式】
一个整数,最少插入的字符数量。
【输入样例】
Ab3bd
【输出样例】
2
【数据规模】
20% 数据满足 s 的长度 ≤10。
100%数据满足s 的长度 ≤5000。
题目类似POJ1159(几乎一样)
此题直接正反相互求一遍LCS就完了,水题。(虽然没有一遍A,zz…)
附AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <set> #include <queue> #include <algorithm> #include <vector> #include <cstdlib> #include <cmath> #include <ctime> #include <stack> #define INF 2100000000 #define ll long long #define clr(x) memset(x,0,sizeof(x)) using namespace std; const int MaxLen = 5005; char a[MaxLen],b[MaxLen]; int len; int DP[MaxLen][MaxLen]; int main(){ freopen("ocd.in","r",stdin); freopen("ocd.out","w",stdout); gets(a+1); len = strlen(a+1); for(int s = 1; s < len; s++) { for(int i = 1; i <= len-s; i++) { int j = i+s; DP[i][j] = 10000; if(a[i] == a[j]) DP[i][j] = DP[i+1][j-1]; DP[i][j] = min(DP[i][j], min(DP[i+1][j]+1, DP[i][j-1]+1)); } } cout << DP[1][len] << endl; return 0; }
相关文章推荐
- poj 1159 palindrome (dp)
- POJ 1159 Palindrome 回文 DP
- POJ 1458 / HDU 1159 / Southeastern Europe 2003 Common Subsequence (DP&LCS)
- POJ 2342 Anniversary Party题解 - 树形DP入门题目
- 【转】POJ 1159 Palindrome【经典的DP回文问题】
- poj1159 dp 滚动数组
- Palindrome(POJ 1159 DP)
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
- poj&nbsp;1159&nbsp;Palindrome(DP&nbsp;空间压缩…
- poj1159_dp
- POJ 1159 Palindrome (滚动数组 DP)
- poj 1159(回文串dp)
- POJ-1159-Palindrome(简单dp)
- POJ1159 - Palindrome(区间DP)
- poj 1159 Palindrome 【DP】
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
- poj&nbsp;1159&nbsp;Palindrome(DP)
- dp四边形优化 poj 1160 Post Office题解
- dp中滚动数组的应用:01背包、POJ 1159 Palindrome、多段图路径问题
- poj 1159 Palindrome(dp)