poj 1159 Palindrome(dp)】的更多相关文章

题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int mmin(int a,int b) { return a>b?b:a; } ][]; int main() { int n,i,j; ]; memset(d,,sizeof(d)); cin>>n; ; i<=n; i++)…
题意:给定一个字符串,让你把它变成回文串,求添加最少的字符数. 析:动态规划是很明显的,就是没有了现思路,还是问的别人才知道,哦,原来要么写,既然是回文串, 那么最后正反都得是一样的,所以我们就正反求LCS,这样公共的就求出来了,那么再用总数减掉这个LCS, 那么剩下的肯定就是没有配对的了,就得必须加上了. 思路有了,这里又出现一个问题,这个求LCS,要用的空间复杂度太大了,MLE了...有了思路,还是过不了, 这个题应该用滚动数组来做,我想想在求LCS时,第一维我们只用到了i-1和i,所以呢,…
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要在该字符串中插入几个字符能是的它变成一个回文串. 分析: 首先把原字符串和它的逆串进行匹配, 找出最长公共子序列. 那么最长公共子序列的字符串肯定是一个回文串. 所以原串剩下的部分是不构成回文的. 我们仅仅须要加入剩下部分的字符到相应位置, 原串自然就变成了一个回文. 所以本题的解为: n 减去 (原串与逆串…
题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为A[0, 1, ..., n],则定义状态dp[i, j]表示字符串A[i, i+1, ..., j]成为回文字符串所需要插入的最少字符数,则当A[i] == A[j+1]时, dp[i,j] = dp[i+1, j-1],否则dp[i, j] = Min(dp[i+1, j], dp[i, j+1…
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->>状态:dp[i][j]表示第i个字符到第j个字符组成的字符串变成回文串的最少插入次数. 状态转移方程: 若sz[i] == sz[j].则:dp[i][j] = dp[i + 1][j - 1]; 否则:dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1; 提交,5…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 59101   Accepted: 20532 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a…
1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 49849   Accepted: 17153 Description A palindrome is a symmetrical string, that is, a strin…
题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output 2 分析:这道题目之前有做过,就是将原字符串逆序得到另一个字符串,求它们的最长公共子序列,这样就能求得它的可以构成回文的最长字符数,用n减去即为添加最少可使之成为回文的数目. 可恨的是之前一直超内存,看了别人的解题报告,原来定义dp[MAX][MAX]时,不用int型,而是short型,内存只占in…
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在这个串中删除最少的字符,使得它回文是一样的. 那么我们可以把这个串reverse,之后的串称为s2,找s2和s的最长公共子序列就好了,因为有了LCS,接着把其他的都删掉,就是一个回文串了,因为正着读和倒着读都一样 还有POJ居然能跑5000^2 我的923MS就跑完了,还是很快的嘛,当然这题还可以滚动数组,…
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in…
任意门:http://poj.org/problem?id=1159 解题思路: LCS + 滚动数组 AC code: #include <cstdio> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define LL long long using namespace std; ; char a[MAXN], b[MAXN]; ][MAXN]; int main() { int l…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 55018   Accepted: 19024 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a…
Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t…
http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数. #include<iostream> #include<string> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; + ; char s1…
Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 59094   Accepted: 20528 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a…
题目传送门 题意:一个字符串要变成回文串至少要插入多少个字符 分析:LCS,长度 - 原串和反串的最大相同长度就是要插入的个数.解释一下,当和反串相同时,在原串中已经是回文的部分了,那么减去LCS长度后剩下的多少个插入多少个一定就能使原串回文dp数组二维都开5000的话就会超内存,这里就用到了滚动数组,因为在LCS的计算中,i的变化只相差1,所以可以通过对2取余来进行滚动:) 收获:原串和反串取LCS能得到部分回文串,推荐继续看:UVA 11404 Palindromic Subsequence…
d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长回文子序列,然后对于原串中不属于这个回文子序列的字符,在串中的相应位置添加一个相同的字符即可.那么需要添加的字符数量即为N-len. 最长回文串可以看作是原串中前面和后面字符的一种匹配(每个后面的字符在前面找到一个符合位置要求的与它相同的字符).这种的回文匹配和原串与逆序串的公共子序列是一一对应的(…
给一字符串,问最少加几个字符能够让它成为回文串. 比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd 思路: 动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几个字符,枚举子串长. if str[i] == str[j]: DP[i][j] = DP[i + 1][j - 1] else: DP[i][j] = min( DP[i + 1][j], DP[i][j - 1] ) + 1 注意: 长度较大为 5000,二维数组 5000 * 5000 须要将…
将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文.. 问题转化为求LCS..但是n最大到5000...裸的LCS需要dp[5000][5000]..显然会爆空间. .而更新的时候之于上一层的数据有关...所以空间不需要开5000*5000...滚动数组..只需2*5000就好... Program: #include<iostream> #include<stdio.h> #inc…
/* * POJ_1159.cpp * * Created on: 2013年10月29日 * Author: Administrator */ #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> using namespace std; const int maxn = 5005; char str1[max…
[POJ 1159]Palindrome 近期各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题另一点须要知道 最少须要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 然而窝原本并不知道--然后写出了一个奇葩dp做法 竟然比LCS快0.0 我的思路是从左往右遍历 每一个字符从右往左遍历到他的后一位置 dp数组标记当前位置往右相应匹配字符串左半边的最长序列长度的两倍(即为要删除序列长度) 每找到一个str[i]==str[j] 更新dp[j]为此时最长序列+2(左…
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: f[x][y]=0  初始化 f[x][y]=f[x+1][y-1]     s[x]==s[y]时 f[x][y]=MIN ( f[x+1][y] , f[x][y-1] )+1    s[x] != s[y]时 代码展示 一开始没有将算的的数据存放到数组中,使得有些数据重复计算好多次,TLE…
Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同时花费为v[i].问最小花费. 以前做过一道类似的题(水库),这道题也差不多.首先来考虑,用\(best[i]\)表示以i为根的子树的最小花费.这样做有什么问题呢?它无法很好的处理消防站重复建的问题. 所以换一种做法.\(best[i]\)依然表示原来的含义,新建一个数组\(f[i][j]\),表示当i这个结…
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 最长公共子串的长度. 额,,这个思路还是不是很好想. LCS: #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; char s1[maxn], s2[maxn]; ][…
  Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 58168   Accepted: 20180 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write…
滚动数组 + LCS // File Name: 1159.cpp // Author: Missa_Chen // Created Time: 2013年07月08日 星期一 10时07分13秒 #include <iostream> #include <string> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #includ…
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+cost[s[i-1]-'a'],dp[i][j-1]+cost[s[j-1]-'a']); if(s[i-1]==s[j-1]) dp[i][j]=min(dp[i+1][j-1],dp[i][j]); 注意循环顺序,我觉得这题就是这里是tricky: #include<iostream> #i…
看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需修改 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i…
Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t…