zoukankan      html  css  js  c++  java
  • UVa 10453 Make Palindrome 字符串dp

    Problem A

    Make Palindrome

    Input: standard input

    Output: standard output

    Time Limit: 8 seconds

    By definition palindrome is a string which is not changed when reversed. "MADAM" is a nice example of palindrome. It is an easy job to test whether a given string is a palindrome or not. But it may not be so easy to generate a palindrome.


    Here we will make a palindrome generator which will take an input string and return a palindrome. You can easily verify that for a string of length 'n', no more than (n-1) characters are required to make it a palindrome. Consider "abcd" and its palindrome "abcdcba" or "abc" and its palindrome "abcba". But life is not so easy for programmers!! We always want optimal cost. And you have to find the minimum number of characters required to make a given string to a palindrome if you are allowed to insert characters at any position of the string.

    Input

    Each input line consists only of lower case letters. The size of input string will be at most 1000. Input is terminated by EOF.


    Output

    For each input print the minimum number of characters and such a palindrome seperated by one space in a line. There may be many such palindromes. Any one will be accepted.

     

    Sample Input

    abcdaaaaabcaababababaabababapqrsabcdpqrs

    Sample Output

    3 abcdcba0 aaaa2 abcba1 baab0 abababaabababa9 pqrsabcdpqrqpdcbasrqp

    Author : Md. Kamruzzaman

    The Real Programmers' Contest-2

    -----------------------

    f[i][j]=f[i+1][j-1]; (s[i]==s[j])当i==j时不需要添加字符

    f[i][j]=min( f[i+1][j], f[i][j-1] )+1; 当i!=j时,1、在j右边添加s[i],2、在i左边添加s[j];

    学会了一个新的存储dp路径的方式。

    p[i][j]==0时表示s[i]==s[j]的情况

    p[i][j]==1表示情况1。p[i][j]==-1表示情况2。

    -------------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    char s[1111];
    int f[1111][1111];
    int q[1111][1111];
    int n;
    
    int dfs(int l,int r)
    {
        if (r-l<1)
        {
            return 0;
        }
        if (f[l][r]!=-1)
        {
            return f[l][r];
        }
        int ret=0;
        if (s[l]==s[r])
        {
            ret=dfs(l+1,r-1);
            q[l][r]=0;
        }
        else
        {
            ret=dfs(l+1,r)+1;
            q[l][r]=1;
            if (dfs(l,r-1)+1<ret)
            {
                ret=dfs(l,r-1)+1;
                q[l][r]=-1;
            }
        }
        f[l][r]=ret;
        return ret;
    }
    
    int main()
    {
        while (cin>>(s+1))
        {
            string str1,str2,str;
            memset(f,-1,sizeof(f));
            memset(q,0,sizeof(q));
            n=strlen(s+1);
            int ans=dfs(1,n);
            int x=1,y=n;
            while (y-x>=0)
            {
                if (q[x][y]==0)
                {
                    if (x==y)
                    {
                        str1+=s[x];
                        x++;
                        y--;
                    }
                    else
                    {
                        str1+=s[x];
                        str2+=s[y];
                        x++;
                        y--;
                    }
                }
                else if (q[x][y]==1)
                {
                    str1+=s[x];
                    str2+=s[x];
                    x++;
                }
                else if (q[x][y]==-1)
                {
                    str1+=s[y];
                    str2+=s[y];
                    y--;
                }
            }
            for (int i=str2.length()-1; i>=0; i--)
            {
                str1+=str2[i];
            }
            cout<<ans<<" "<<str1<<endl;
        }
        return 0;
    }
    





  • 相关阅读:
    P3932 浮游大陆的68号岛
    P4595 [COCI2011-2012#5] POPLOCAVANJE
    CF455E Function
    【转载】乱堆的东西
    BZOJ1034 [ZJOI2008]泡泡堂BNB[贪心]
    CSP2019退役记
    BZOJ5206 [Jsoi2017]原力[根号分治]
    luogu3651 展翅翱翔之时 (はばたきのとき)[基环树+贪心]
    BZOJ1040 [ZJOI2008]骑士[基环树DP]
    BZOJ3037 创世纪[基环树DP]
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226390.html
Copyright © 2011-2022 走看看