poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)

Palindrome

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 53414   Accepted: 18449

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

Source

IOI 2000

题目大意:添加最少的字符,使得字符串变为回文串

如果一个字符串是回文串,那么它与它的逆序数组的最长公共子序列为自身的长度,所以求出最长公共子序列的长度后,用总的长度减去它,得到的就是要修改的长度。

使用short的5000*5000的数组

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
short dp[5100][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
    int i , j , n ;
    while(scanf("%d", &n) !=EOF)
    {
        scanf("%s", str1);
        for(i = 0 ; i < n ; i++)
            str2[n-1-i] = str1[i] ;
        str2[i] = '\0' ;
        for(i = 1 ; i <= n ; i++)
            for(j = 1 ; j <= n ; j++)
            {
                if( str1[i-1] == str2[j-1] )
                    dp[i][j] = dp[i-1][j-1]+1 ;
                else
                    dp[i][j] = max( dp[i-1][j],dp[i][j-1] );
            }
        printf("%d\n", n-dp[n][n]);
    }
    return 0;
}

使用滚动数组

滚动数组:使用两行数组,模拟大的二维数组

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[2][5100] ;
char str1[5100] , str2[5100] ;
int main()
{
    int i , j , n , k ;
    while(scanf("%d", &n) !=EOF)
    {
        scanf("%s", str1);
        for(i = 0 ; i < n ; i++)
            str2[n-1-i] = str1[i] ;
        str2[i] = '\0' ;
        k = 0 ;
        for(i = 1 ; i <= n ; i++)
        {
            k = 1 - k ;
            for(j = 1 ; j <= n ; j++)
            {
                if( str1[i-1] == str2[j-1] )
                    dp[k][j] = dp[1-k][j-1]+1 ;
                else
                    dp[k][j] = max( dp[1-k][j],dp[k][j-1] );
            }
        }
        printf("%d\n", n-dp[k][n]);
    }
    return 0;
}
时间: 2024-10-24 14:47:45

poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)的相关文章

HDU 1513 Palindrome(最长公共子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 解题报告:给定一个长度为n的字符串,在这个字符串中插入最少的字符使得这个字符串成为回文串,求这个最少的个数是多少? 一开始以为只是一个普通的DP题,但是按照我的想法敲出来之后怎么样都W了,无奈搜了解题报告,得知其实这个就是一个最长公共子序列问题,就是求这个字符串跟它的逆序的 字符串的最长公共子序列.因为杭电的题内存都要求在32M内存以内,所以很开心的敲出来才发现10^6的数组都开不了,所以只好

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfille

POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 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

uva 11151 Longest Palindrome (最长公共子序列)

uva 11151 Longest Palindrome A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-p

hdu 1159 Common Subsequence(dp 最长公共子序列问题LCS)

最长公共子序列问题(LCS,Longerst Common Subsequence). s1s2……si+1和t1t2……tj+1的公共子序列可能是: ①当si+1=tj+1时,在s1s2……si+1和t1t2……tj+1的公共子序列末尾追加一个. ②s1s2……si+1和t1t2……tj的公共子序列 ③s1s2……si和t1t2……tj+1的公共子序列 所以易得到递推关系dp[i+1][j+1]=  max{ dp[i][j]+1 , dp[i][j+1] , dp[i+1][j]) }  

UVA 10723--Cyborg Genes+最长公共子序列变形

题目链接:点击进入 首先对于长度最短的情况是很容易确定的,只需要用两个字符串的长度和减去他们的最长公共子序列长度.然后比较麻烦的就是合乎要求的字符串的个数,其实我们也可以用类似于最长公共子序列的dp来求. 设dp[i][j]表示str1的前i个字符和str2的前j个字符所得到的满足要求的字符串,则如果str[i]==str[j],则dp[i][j]+=dp[i-1][j-1]; 否则就要根据i,j这两个位置上的最长公共子序列长度进行讨论,具体见代码. 代码如下: #include<iostrea

hdu1243(最长公共子序列变形)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个字母值为1,这里每个字母代表的值变化了一下. 状态转移方程:if(s1[i-1]==s2[j-1])dp[nxt][j]=dp[cur][j-1]+val[s1[i-1]];                              else  dp[nxt][j]=max(dp[nxt][j-1]

51Nod 1092 回文字符串 | 最长公共子序列变形

求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #define maxn 1005 char s[maxn],s1[maxn]; int dp[maxn][maxn]; int main() { int n=0,i,j,len; scanf("%s",s); len=strlen(s); strcpy(s1,s); strrev(s1); f

hdu1159 dp(最长公共子序列)

题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根据比较情况相同与否确定递推关系: dp [ i + 1 ] [ j + 1 ] 表示匹配到 a 字符串的第 i 个字符和 b 字符串的第 j 个字符时的最大匹配数,由于读字符串的时候我是从下标 0 读起的,但我需要用 dp [ 0 ] ,所以就都是加了一,否则也可以读入的时候直接从 a + 1 和