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





  • 相关阅读:
    Kubernetes 1.5部署sonarqube
    Kubernetes 1.5集成heapster
    Kubernetes 1.5 配置dashboard
    SQL SERVER中的逻辑读取,物理读取,以及预读的理解
    JS控制显示/隐藏二级菜单
    Css下拉菜单设置
    div包裹页面后多余部分没有显示,也没滚动条 overflow 属性设置
    Sql Ado.net 学习笔记之连接字符串
    Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
    SQL中的字母的大小写转换
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226390.html
Copyright © 2011-2022 走看看