poj1159 dp

H - 简单dp 例题扩展

Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a 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

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from ‘A‘ to ‘Z‘, lowercase letters from ‘a‘ to ‘z‘ and digits from ‘0‘ to ‘9‘. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5
Ab3bd

Sample Output

2题目大意:给你一个字符串,让你往其中插入字符,使其变成回文字符串,问最少需要插入多少字符。思路分析:所谓回文字符串,就是正读和倒读一样的,因此可以考虑构造两个字符串,另一个字符串是原字符串的逆序串,然后求最长公共子序列长度,那么字符串长度减去最长公共字符串长度就是需要插入的字符数,注意dp数组开short,否则会爆内存。代码:#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <stack>using namespace std;const int maxn=5000+100;short dp[maxn][maxn];int main(){    int n;    char s1[maxn],s2[maxn];    scanf("%d",&n);       cin>>s1;       for(int i=n-1;i>=0;i--)        s2[n-i-1]=s1[i];       memset(dp,0,sizeof(dp));       for(int i=0;i<n;i++)       {           for(int j=0;j<n;j++)           {               if(s1[i]==s2[j])               {                   if(i>=1&&j>=1)                    dp[i][j]=dp[i-1][j-1]+1;                   else dp[i][j]=1;               }               if(s1[i]!=s2[j])               {                   if(i==0&&j==0) dp[0][0]=0;                   else if(i>=1&&j==0) dp[i][j]=dp[i-1][j];                   else if(i==0&&j>=1) dp[i][j]=dp[i][j-1];                   else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);               }           }       }       cout<<(n-dp[n-1][n-1])<<endl;    return 0;}
时间: 2024-10-10 15:06:30

poj1159 dp的相关文章

poj1159 dp最长公共子串

1 //Accepted 204 KB 891 ms 2 //dp最长公共子串 3 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) 4 //dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1) (s1[i]==s2[j]) 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 using namespace std; 9 const int im

区间DP基础篇之 POJ1159——Palindrome

题目大意:给定一个字符串,求最少插入几个字符让该字符串成为回文串 法一: dp[i][j]表示使区间[i,j]成为回文串最小插入的字符数,则状态转移方程 1.if s[i]==s[len-1] 则:d[i][j]=d[i+1][j-1] 2.else  d[i]=min(dp[i+1][j],dp[i][j-1]) 首尾字符不同的时候,有两种决策. 1.将新字符插在首位,那么状态就变成了dp[i+1][j]了. 2.将新字符插在末尾,则状态就变成了dp[i][j-1]了 .比较两种决策哪种更优就

POJ1159:Palindrome【dp】

题目大意:给出一个字符串,问至少添加多少个字符才能使它成为回文串? 思路:很明显的方程是:dp[i][j]=min{dp[i+1][j],dp[i][j-1],dp[i+1][j-1](str[i]==str[j]时)} dp[i][j]表示第i个字符到第j个字符构造成回文串最少添加的字符,但discuss里说了 这样做会超空间+超时 观察下这个方程,每次用到的就是那三个子状态,于是适当的改变方程的形式: dp[i][j]表示第i个字符开始长度为j的子串构成的字符串最少需要添加的字符,于是方程变

[POJ1159]Palindrome(dp,滚动数组)

题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题卡内存真稀奇,于是修改成滚动数组.观察发现i值的更新只有可能是从i或i-1转移来,所以就i取模2. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏

poj1159(dp)

题目链接:http://poj.org/problem?id=1159 Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56628   Accepted: 19577 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as fro

DP总结 ——QPH

常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一头压入元素. 队列中维护的是两个值.一个是位置,这和k的范围有关系,另外一个是f(k)的值,这个用来维护单调性,当然如果f(k)的值可以利用dp值在O(1)的时间内计算出来的话队列中可以只维护一个表示位置的变量. 枚举到一个i的时候,首先判断队首元素的位置是否已经不满足k的范围了,如果不满足就将队首

poj1159——回文,lcs

poj1159——回文,lcs Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 54621   Accepted: 18892 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You

POJ1159——Palindrome(最长公共子序列+滚动数组)

Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inser

POJ1159 Palindrome 【动态规划】

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 52571   Accepted: 18124 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a