POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS)
id=1159">http://poj.org/problem? id=1159
题意:
给你一个字符串, 问你做少须要在该字符串中插入几个字符能是的它变成一个回文串.
分析:
首先把原字符串和它的逆串进行匹配, 找出最长公共子序列. 那么最长公共子序列的字符串肯定是一个回文串. 所以原串剩下的部分是不构成回文的. 我们仅仅须要加入剩下部分的字符到相应位置, 原串自然就变成了一个回文.
所以本题的解为: n 减去 (原串与逆串的LCS长度).
令dp[i][j]==x表示串A的前i个字符与串B的前j个字符的子串的最长公共子序列LCS.
初始化: dp全为0.
状态转移:
A[i]==B[j]时: dp[i][j] = dp[i-1][j-1]+1.
A[i]!=B[j]时: dp[i][j] = max( dp[i-1][j] , dp[i][j-1] ).
终于所求: dp[n][m].
程序实现用的2维滚动数组, 假设用int[5000][5000]会超内存.
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5000+5; int n;
char s1[maxn],s2[maxn];
int dp[2][maxn]; int main()
{
while(scanf("%d",&n)==1)
{
scanf("%s",s1);
for(int i=0;i<n;i++)
s2[i]=s1[n-1-i];
memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(s1[i-1]==s2[j-1])
dp[i%2][j]=dp[(i-1)%2][j-1]+1;
else
dp[i%2][j]=max(dp[(i-1)%2][j] , dp[i%2][j-1]);
}
printf("%d\n",n-dp[n%2][n]);
}
return 0;
}
POJ 1159 Palindrome(字符串变回文:LCS)的更多相关文章
- 1159 Palindrome(最小插入回文串)
标题效果 定的字符串长度的串和内容.中的字符可以在任何位置被插入.它至少需要为数字,这使得编程回文串串. 回文序列从左至右,从右到左和读取相同. 例如. aaaacbbbb它是一个回文串 aaab前面 ...
- 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...
- 动态规划+滚动数组 -- POJ 1159 Palindrome
给一字符串,问最少加几个字符能够让它成为回文串. 比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd 思路: 动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几 ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- poj - 1159 - Palindrome(滚动数组dp)
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- java判断字符串是否回文
java判断字符串是否回文 /** * java判断字符串是否回文<br><br> * 基本思想是利用字符串首尾对应位置相比较 * * @author InJavaWeTrus ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- CRegKey
1.简介 CRegKey提供了对系统注册表的操作方法,通过CRegKey类,可以方便的打开注册表的某个分支或子键(CRegKey::Open),可以方便的修改一个键的键值(CRegKey::SetVa ...
- php 阿里云短信验证码
阿里云短信服务:https://dysms.console.aliyun.com 1.准备 1.1.创建签名.模板 1.2.创建.使用阿里云秘钥 地址:https://usercenter.conso ...
- C3P0连接池参数配置说明
C3P0连接池参数配置说明 created by cjk on 2017.8.15 常用配置 initialPoolSize:连接池初始化时创建的连接数,default : 3(建议使用) minPo ...
- JavaScript中的跨域问题
跨域问题其实很普遍的存在的,如何解决跨域问题呢,跨域问题到底是怎么产生的,解决方法的由来又是什么?我觉得看了视频讲解,值得写下来,记录下来. 一.跨域问题是怎么产生? 概念:只要协议.域名.端口有任何 ...
- Go:方法
一.基本介绍 在某些情况下,我们需要定义方法.比如 Person 结构体,除了有一些字段外(姓名.年龄...),还可以有一些行为动作(吃.唱歌...),这就需要用方法才能实现. Go中的方法是作用在指 ...
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- Maximun product
Given a sequence of integers S = {S1, S2, ..., Sn}, you shoulddetermine what is the value of the max ...
- flash-热风焊盘的制作
计算部分: 热风焊盘的内径(ID)等于钻孔直径+20mil, 外径(OD)等于Anti-pad的直径,通常是比焊盘的直径大20mil. 开口宽度等于(OD-ID)/2+10mil,保留整数位. 如果焊 ...
- winfrom Panel 问题
Panel 图片自适应 BackgroundImageLayout 设置成Zoom就行 Panel动态换图片时候的闪烁问题: 首先创建一个自己的panel类: using System; using ...
- PowerShell Tools for Visual Studio 2015
首先要去下载Visual Studio 2015 RC 版本 https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downl ...