(POJ 1159)Palindrome <最长公共子序列 / 滚动数组优化 / 记忆化搜索> 简单回文数

来源:互联网 发布:淘宝客违规推广 编辑:程序博客网 时间:2024/06/02 05:58

Palindrome
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

题意:
给你一个n个字符的字符串,问把它变成一个回文数最少要插入多少个字符?

分析:
思路一: 将字符串s1倒过来保存在s2中求s1,s2的最长公共子序列数为num,则答案为 n-num
注意:n<= 5000 很大,我们可以用 short int dp[][] 或者是利用滚动数组进行优化

最长公共子序列:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 5010;char s1[maxn],s2[maxn];short int dp[maxn][maxn];//最长公共子序列int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        scanf("%s",s1);        for(int i=n-1,j=0;i>=0;i--,j++)            s2[j] = s1[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][j] = dp[i-1][j-1] + 1;                else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);            }        }        int ans = n - dp[n][n];        printf("%d\n",ans);    }    return 0;}

滚动数组优化:

#include <iostream>#include <cstdio>#define sz 5000using namespace std;int dp[2][sz+1];char s1[sz+2],s2[sz+2];int main(){    int n,i,j,e = 1;    scanf("%d",&n);    scanf("%s",s1+1);    for(i = 1; i <= n; ++i)    {        s2[i] = s1[n-i+1];    }    dp[0][0] = 0;    for(i = 1; i <= n; ++i,e^=1)//e 1 0 1 0变换 达到"滚"的效果    {        for(j = 1; j <= n; ++j)        {            if(s1[i] == s2[j]) dp[e][j] = dp[e^1][j-1]+1;            else dp[e][j] = max(dp[e][j-1],dp[e^1][j]);        }    }    printf("%d\n",n-dp[e^1][n]);    return 0;

思路二:
对于一个字符串序列S1,S2,S3……Sn 想要将它变成回文的,注意!!不要想得太复杂,其实我们只能在字符串的两端添加字符(这儿要仔细想想,想通了,这题就变得容易了),在字符串当中添加字符是完全没有意义的,所以对于当前的状态,我们只可能遇到2种可能的情况,我们来归纳一下:
1.当S1==Sn时(字符串头字符和字符串尾部字符相等时),我们的任务便转换为了将S2,S3,S4……S(n-1)变成回文,对吗?
2.当S1!=Sn时,我们又有了两种决策
第一种决策:在字符串序列S1,S2,S3……Sn 的左边添加一个字符,我们设这个字符为Si,使它等于Sn,这样我们就将当前的任务转化为了将S1,S2,S3……S(n-1)变成回文字符串。
第二种决策:在字符串序列S1,S2,S3……Sn 的右边添加一个字符,我们设这个字符为Sk,使他等于S1,这样我们就将当前的任务转化为了将S2,S3,S4……Sn变成回文字符串。

根据这个思路,我们很快就能写出递归代码

记忆化搜索:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 5010;char s1[maxn],s2[maxn];short int dp[maxn][maxn];//记忆化搜索int dfs(int l,int r){    if(l >= r) return dp[l][r] = 0;    if(dp[l][r] != -1) return dp[l][r];    int tmp;    if(s1[l] == s1[r]) tmp = dfs(l+1,r-1);    else tmp = min(dfs(l+1,r)+1,dfs(l,r-1)+1);    return dp[l][r] = tmp;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        scanf("%s",s1+1);        memset(dp,-1,sizeof(dp));        printf("%d\n",dfs(1,n));    }    return 0;}
1 0
原创粉丝点击