LightOJ 1033 - Generating Palindromes 【区间DP】
2015-06-10 15:56
441 查看
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1033
题意:至少添加几个字符,能使得给定的串变为回文串。
解法:枚举起点终点,进行DP;
代码:
题意:至少添加几个字符,能使得给定的串变为回文串。
解法:枚举起点终点,进行DP;
代码:
[code]#include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include <iterator> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <list> #include <bitset> #include <sstream> #include <iomanip> #include <fstream> #include <iostream> #include <ctime> #include <cmath> #include <cstring> #include <cstdio> #include <time.h> #include <ctype.h> #include <string.h> #include <assert.h> using namespace std; int t; char s[1010]; int dp[1010][1010]; int main() { scanf("%d",&t); for (int ca = 1; ca <= t;ca++) { memset(dp,0,sizeof(dp)); scanf("%s",s+1); int n = strlen(s+1); for (int i = n; i >= 1; i--) for (int j = i+1; j <= n; 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; } printf("Case %d: %d\n",ca,dp[1] ); } return 0; }
相关文章推荐
- Optimizing Map/Reduce with MongoDB
- Unity之Bmob云存储一
- PHP PSR-1 基本代码规范(中文版)
- 查看运行中的Windows进程是32位还是64位的方法
- 【存储】Cocos2d-x使用JsonCpp
- 跳跃表实现的原理
- 数据分析师面试题攻略
- 无oralce客户端pl/sql Dev 如何连接oracle
- monkey 相关命令
- java Enumeration用法
- mysql5.5手册读书日记(3)
- IOS之OC中的类
- Mybatis学习(一)原生态的JDBC编程总结
- 阿里云服务器验证码不能显示解决办法java.lang.Error: Probable fatal error:No fonts found
- php 排除周末与节假日程序实例代码
- 二路归并排序
- 贪心 Codeforces Round #109 (Div. 2) B. Combination
- PHP PSR-3 日志接口规范 (中文版)
- 网络对时程序实例
- 科普知识:Windows下的Program Files (x86)文件夹是干什么的?