51nod 1092 回文字符串 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092
这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i][j] 为把以i开头j结尾的子串变为回文串的最少次数,
if(s[i]==s[j]) dp[i][j]=dp[i+1][j-1];
else dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 1010
#define mod 1000000000
using namespace std; int dp[N][N];
char s[N]; int main()
{
//Read();
scanf("%s",s);
int m=strlen(s);
memset(dp,,sizeof(dp));
for(int i=m-;i>=;i--)
{
for(int j=i+;j<m;j++)
{
if(s[i]==s[j]) dp[i][j]=dp[i+][j-];
else dp[i][j]=min(dp[i+][j],dp[i][j-])+;
//printf("%d\n",dp[i][j]);
}
}
printf("%d\n",dp[][m-]);
return ;
}
还可以转化为最长公共子序列的问题求解,求最少变成回文串的操作次数,就是求有几个字符与对应的字符不一样,那么只要我把原字符翻转,然后求最长公共子序列,
用字符串长度减去公共子序列长度即可求解。
因为dp[i+1]计算时只用到了dp[i],dp[i+1],所以可以结合奇偶性用滚动数组优化空间。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 1010
#define mod 1000000000
using namespace std; int dp[][N];
char s[N],ss[N]; int main()
{
//Read();
scanf("%s",s);
int m=strlen(s);
for(int i=;i<m;i++)
ss[i]=s[m-i-];
ss[m]='\0';
memset(dp,,sizeof(dp));
for(int i=;i<m;i++)
{
for(int j=;j<m;j++)
{
if(s[i]==ss[j]) dp[(i+) & ][j+]=dp[i & ][j]+;
else dp[(i+) & ][j+]=max(dp[i & ][j+],dp[(i+) & ][j]);
//printf("%d\n",dp[i+1][j+1]);
}
}
// printf("%d\n",dp[m][m]);
printf("%d\n",m-dp[m&][m]);
return ;
}
51nod 1092 回文字符串 (dp)的更多相关文章
- 51nod 1092 回文字符串【LCS】
1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符 ...
- 51Nod - 1092 回文字符串(添加删除字符LCS变形)
回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca, ...
- 51Nod 1092 回文字符串(LCS + dp)
51Nod 1092 数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC. 1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目.(子序列:可以不连续) ...
- 51NOD 1092 回文字符串 LCS
Q:给定一个串,问需要插入多少字符才能使其成为回文串,也就是左右对称的串. 经典求LCS题,即最长公共子序列,不用连续的序列.考虑O(n^2^)解法,求LCS起码得有两个串,题中才给了一个串,另一个需 ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...
- 51Nod 1092 回文字符串
最开始毫无头绪,然后参照了一位dalao的博客,思路是一个正序的字符串将其逆序,然后求最长公共子序列(LCS),emm也属于动态规划. #include <iostream> #inclu ...
- 1042 数字0-9的数量 1050 循环数组最大子段和 1062 序列中最大的数 1067 Bash游戏 V2 1092 回文字符串
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次 ...
- 51 Nod 1092 回文字符串
1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每 ...
- 1092 回文字符串(LCSL_DP)
1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符 ...
随机推荐
- hdu 3123 GCC 阶乘
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123 The GNU Compiler Collection (usually shortened t ...
- 【DP】permu
permu [Description] 给定两个1~N的全排列A,B.有两个指针q和p,一开始q.p都为0,可执行以下三种操作: 1.q+1:2.p+1:3.q+1且p+1(Aq+1≠Bp+1时才可以 ...
- matrix_world_final_2012
B http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98759#problem/B 题意:瓶子侧躺在数轴上,瓶底在xlow,瓶口在xhigh ...
- 关灯问题 dp
题意是一排路灯,每个路灯有耗电量,照明度,需要给这n个路灯按顺序分组,每组内的最大耗电量是电灯数乘t,可以选择关闭一些电灯,求最大的照明度: 这题思路很明显,预处理出一个g[i][j]表示i到j分为一 ...
- rivers ioi2005 树形dp
说句实话,写完这道题,很想吐一口血出来,以示我心情的糟糕: 题目很简单,树形dp,正常做30分钟,硬是做了好几个小时,真是伤心. 题解不写了,只是吐个槽,网上没有用背包写的dp,全是左儿子右兄弟写法, ...
- Sencha Touch2 时间轴ListPanel
直接贴代码 timeline.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...
- 腾讯开源的轻量级CSS3动画库:JX.Animate
JX.Animate 是由腾讯前端团队 AlloyTeam 推出的一个 CSS3 动画库,通过 JX(腾讯的前端框架)插件的形式提供. Why CSS3 众所周知在支持HTML5的浏览器中 ...
- 帝国cms栏目别名如何调用?
我们在用帝国cms建站时经常会发现栏目的标题不好设置,栏目名称太长的话在后台那边看了眼花,太短又不好优化.能不能直接调用栏目别名呢?栏目别名不会什么影响.那么,帝国cms栏目别名怎么调用呢?和ytka ...
- EF 中更新模型的问题,这种错误(因为相同类型的其他实体已具有相同的主键值。)
在EF经常在更新模型的时候可能会同时操作一个实体几次. 其实除了SaveChanges外,其它的几次基本都是要查询出一个结果, 例如更新的时候,我们要查一下这个表中有没有相同的纪录之类的. 查询完之后 ...
- Flex +WebService
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...