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 ;
}

最新文章

  1. pthon在Notepad++中执行方式
  2. 【FastJSON】解决FastJson中“$ref 循环引用”的问题
  3. 修改jmeter jvm参数
  4. 表单同时有中文字段和文件上传,加上enctype=&quot;multipart/form-data&quot;后导致的中文乱码问题
  5. Poj 2092 Grandpa is Famous(基数排序)
  6. phpcms 模板常用标签指南
  7. 《Linux命令行与shell脚本编程大全》 第八章管理文件系统
  8. Ubuntu下的Samba服务器配置
  9. HttpURLConnection 411错误解决
  10. Ceva定理的四种证明方法
  11. Finance公式说明
  12. java发送邮件高级篇
  13. CentOS7下 让Docker pull命令使用squid做http代理拉取目标镜像仓库的镜像
  14. nmap扫描内网存活机器脚本
  15. 工作所用的日常 Git 命令
  16. 24最小生成树之Prim算法
  17. hbase读写流程分析
  18. 基于socketserver实现并发
  19. 传递的值是this,在js里就不用再写$(this)
  20. Hadoop-2.2.0中文文档—— Common - 超级用户模拟别的用户

热门文章

  1. 【CentOs】开机启动与防火墙
  2. Leetcode#127 Word Ladder
  3. 【补解体报告】topcoder 634 DIV 2
  4. C++时间标准库时间time和系统时间的使用
  5. APM 终端用户体验监控分析(上)
  6. 《Thinking in C++》学习笔记(二)【第三章】
  7. SQL技术内幕-10 in和exists 性能比较
  8. JDBC 程序的常见错误及调试方法
  9. 正则表达式(RegExp)
  10. 经典SQL查询语句大全