集训第五周动态规划 G题 回文串
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 这道题类似于编辑距离,使用动态规划可解: 用dp(i,j)表示数组从i到j的这个区间形成的子串,使其成为回文串需要加入最小的字符个数 那么动态规划方程为
1.min(dp(i+1,j)+1,dp(i,j-1)+1) (a[i]!=a[j])
dp(i,j)={
2.dp(i+1,j-1) (a[i]=a[j]) 这道题由于n最大可达5000,因此使用int型的二维数组会超空间,可以把int型改成short型,同时也可以使用滚动数组,因为这个状态转移方程是在两个相邻的
状态之间转变,因此使用两行一列的数组就完全可以存下,而且这个DP也类似于递推,由已知推未知,本就用不到那么多的空间,可以覆盖掉那些没用的,只保存有用的就行。
#include"iostream"
#include"cstring"
using namespace std;
const int maxn=5010;
char a[maxn];
int dp[2][maxn],n;
void Init()
{
for(int i=1;i<=n;i++) {cin>>a[i];}
memset(dp,0,sizeof(dp));
}
void Work()
{
for(int i=n-1;i>=1;i--)
{
for(int j=i+1;j<=n;j++)
{
if(a[i]==a[j])
dp[i%2][j]=dp[(i+1)%2][j-1];
else
dp[i%2][j]=min(dp[(i+1)%2][j],dp[i%2][j-1])+1;
}
}
}
void Print()
{
cout<<dp[1][n]<<endl;
}
int main()
{
while(cin>>n)
{
Init();
Work();
Print();
}
return 0;
}
#include"iostream"
#include"cstring"
using namespace std; const int maxn=;
char a[maxn];
short dp[maxn][maxn],n; void Init()
{ for(int i=;i<=n;i++) {cin>>a[i];}
memset(dp,0x3f,sizeof(dp));
} void Work()
{
for(int i=;i<=n;i++) {dp[i][i]=;dp[i+][i]=;}
for(int i=n-;i>=;i--)
{
for(int j=i+;j<=n;j++)
{
if(a[i]==a[j])
dp[i][j]=dp[i+][j-];
else
dp[i][j]=min(dp[i+][j],dp[i][j-])+;
}
}
} void Print()
{
cout<<dp[][n]<<endl;
} int main()
{
while(cin>>n)
{
Init();
Work();
Print();
}
return ;
}
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=+;
char a[maxn];
short dp[maxn][maxn];
int main()
{
int n;
while(cin>>n)
{
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++)
{
dp[i-][i]=;
dp[i][i]=;
}
for(int i=;i<=n;i++)
for(int j=i-;j>=;j--)
{
if(a[i]==a[j]) dp[i][j]=dp[i-][j+];
else dp[i][j]=min(dp[i-][j]+,dp[i][j+]+);
}
cout<<dp[n][]<<endl;
}
return ;
}
集训第五周动态规划 G题 回文串的更多相关文章
- 集训第五周动态规划 H题 回文串统计
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...
- 集训第五周动态规划 D题 LCS
Description In a few months the European Currency Union will become a reality. However, to join the ...
- 集训第五周动态规划 C题 编辑距离
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- 集训第五周 动态规划 B题LIS
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- 集训第五周动态规划 I题 记忆化搜索
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- 集训第五周动态规划 F题 最大子矩阵和
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- 集训第五周 动态规划 K题 背包
K - 背包 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 集训第五周动态规划 J题 括号匹配
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- 集训第五周动态规划 E题 LIS
Description The world financial crisis is quite a subject. Some people are more relaxed while others ...
随机推荐
- _bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 见一周目记录:http://www.cnblogs.com/ciao-sora/p/6 ...
- bzoj2581 [USACO 2012 Jan Gold] Cow Run【And-Or Tree】
传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=110 传送门2:http://www.lydsy.com/JudgeOn ...
- 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...
- ASP.NET MVC 实现伪静态
1 什么是伪静态? 现在很多门户网站或者各大电商平台的网站的链接最后都是.htm或者.htm结尾,那么他们的网页真的是静态的html吗?拿京东来说,有无数个页面都都Html,在商品每时每刻都可能被更 ...
- IOS 绘制PDF -转
-(void)createPdf:(UIImage *)img andText:(NSString *)text{ NSArray *paths = NSSearchPathForDirectorie ...
- 转】在Ubuntu中安装Redis
不多说,直接上干货! 原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/ 感谢! 在Ubuntu中安装Redis R利剑 ...
- Codeforces Round #138 (Div. 1)
A 记得以前做过 当时好像没做对 就是找个子串 满足括号的匹配 []最多的 开两个栈模拟 标记下就行 #include <iostream> #include<cstring> ...
- 学JAVA第二十二天,StringBuffer的好处
五一的假期今天就结束了,又要回来上课了. 今天就写一下StringBuffer的好处吧. StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象. 也就是说,我们平时用String ...
- Discrete Logging
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5865 Accepted: 2618 ...
- linux 安装 mongo 3.4
要求:linux 安装 mongo 3.4 大体上,按照官网提供的方法来做. 系统是ubuntu 16.04 安装的是mongo3.4.8 社区版 1. 导入导入包管理系统使用的公钥 ...