POJ 1159 && HDU 1513 Palindrome(LCS)
来源:互联网 发布:淘宝网如何找同款 编辑:程序博客网 时间:2024/06/05 05:41
Description
给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串
Input
第一行一个整数n表示字符串长度(n<=5000),第二行一个字符串
Output
输出能使原字符串变为回文字符串最少需要添加几个字符
Sample Input
5
Ab3bd
Sample Output
2
Solution
设原序列S的逆序列为S’,则最少需要补充的字母数=原序列S的长度-S和S’的最长公共子串长度,故转化为LCS问题
注意:本题空间开销较大,需特殊处理,下面给出两种节省空间的方法
1.静态数组(用short int)
2.滚动数组
Code
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define INF (1<<29)#define maxn 5005char s1[maxn],s2[maxn];int n;int dp[2][maxn];int main(){ while(~scanf("%d%s",&n,s1)) { for(int i=0;i<n;i++)//求逆串 s2[n-i-1]=s1[i]; memset(dp,0,sizeof(dp));//初始化 for(int i=0;i<n;i++)//LCS for(int j=0;j<n;j++) { if(s1[i]==s2[j]) dp[(i+1)%2][j+1]=dp[i%2][j]+1; else dp[(i+1)%2][j+1]=max(dp[i%2][j+1],dp[(i+1)%2][j]); } cout<<n-dp[n%2][n]<<endl; } return 0;}
0 0
- POJ 1159 && HDU 1513 Palindrome(LCS)
- poj 1159 && hdu 1513 Palindrome(LCS)
- POJ 1159 Palindrome (LCS)
- POJ 1159 Palindrome (LCS)
- hdu 1513 Palindrome(LCS)
- HDU 1513 Palindrome(LCS)
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- poj 1159 Palindrome (LCS)
- Palindrome POJ 1159 【LCS】
- POJ 1159-Palindrome(DP/LCS变形)
- POJ 1159 Palindrome(LCS~~回文)
- POJ - 1159 - Palindrome (LCS + 优化)
- HDU 1513 Palindrome(LCS+滚动数组)
- HDU 1513 Palindrome (动态规划 & LCS)
- POJ-1159-Palindrome-lcs扩展
- HDU Problem 1513 Palindrome 【LCS】
- POJ 1159 Palindrome(lcs加滚动数组)
- CSS3 display:flex和display:box有什么区别?
- Codeforces Round #308 (Div. 2)
- 第二十四讲 if语句的嵌套
- Rom开发 视频播放器开发
- 02_Android写xml文件和读xml文件
- POJ 1159 && HDU 1513 Palindrome(LCS)
- 设置UIView的位置和尺寸
- 回复吧~这样我就能快速加经验了!
- 内部排序(五)基数排序
- PHP5.2至5.6的新增功能详解
- 对新手在使用jni不怎么明白的一些指导;我看看来几处这方面的教程写的,相当于用自己的语言转述一下
- ASP.NET环境下配置FCKEditor并上传图片及其它文件
- 剑指offer35:第一个只出现一次的字符(hash表使用)
- 软件壳