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;
    }
    





  • 相关阅读:
    XAML实例教程系列 依赖属性和附加属性
    分享Silverlight/Windows8/WPF/WP7/HTML5周学习导读(6月4日6月10日)
    QT GUI基本布局
    mqtt client libraries for c
    QT sqlite相关操作
    navicat 激活工具激活时必须断网 ,如果没有断网激活 激活过程中报如下错误 请卸载navicat 重新安装再行激活操作
    vmware 16 windows7企业版 tools安装不了 驱动签名验证
    虚拟机复制
    Install systemtap on Ubuntu 12.04
    DevOps的各个阶段
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226390.html
Copyright © 2011-2022 走看看