码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1159 Palindrome

时间:2014-07-15 12:36:31      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   strong   

<table border="0" width="100%" background="http://poj.org/images/table_back.jpg" style="font-family: Simsun; "><tbody><tr><td><div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue; ">Palindrome</div><div class="plm" style="text-align: center; font-size: 12pt; "><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 3000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536K</td></tr><tr><td><strong>Total Submissions:</strong> 51799</td><td width="10px"> </td><td><strong>Accepted:</strong> 17834</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue; ">Description</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">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. 
</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue; ">Input</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">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.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue; ">Output</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt; ">Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue; ">Sample Input</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt; ">5
Ab3bd

Sample Output

2

Source


思路 最小增加字符数会等于该字符串的长度减去该字符串与其逆序字符串的最长公共子序列的长度

简单动归 
当str[i]==str[j]时
dp[i][j]=dp[i-1][j-1]
否则
dp[i][j]=1+min(dp[i+1][j],dp[i][j+1])
注意此题直接用int定义dp[5000][5000]会MLE   可以用short int定义  
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str1[5005],str2[5005];
short int len[5005][5005];
int main()
{
    int n,i,j,l,m;
    while(~scanf("%d",&n))
    {
        memset(len,0,sizeof len);
        scanf("%s",&str1);
        for(i=0;i<n;i++)
            str2[i]=str1[n-i-1];
            for(int i=0;i<n;++i)
    {
        for(int j=0;j<n;++j)
        {
            if(i==0 || j==0)//ij为0时必须小心处理。如果只考虑a[i]==b[j]情况,当不相等时,就会调用max(a[i-1],b[j-1])就越界了。
            {
                if(str1[i]==str2[j])
                {
                    len[i][j]=1;
                }
                else if(i==0 && j!=0)
                {
                    len[i][j]=len[i][j-1];
                }
                else if(i!=0 && j==0)
                {
                    len[i][j]=len[i-1][j];
                }
                else if(i==0 && j==0)
                {
                    len[i][j]=0;
                }

            }
            else if(str1[i]==str2[j])
            {
                len[i][j]=len[i-1][j-1]+1;
            }
            else
            {
                len[i][j]=max(len[i-1][j],len[i][j-1]);
            }
        }
    }
        printf("%d\n",n-len[n-1][n-1]);

    }

    return 0;
}
另付滚动数组加动归题解链接   <a target=_blank href="http://user.qzone.qq.com/289065406/blog/1300587979">点击打开链接</a>

poj 1159 Palindrome,布布扣,bubuko.com

poj 1159 Palindrome

标签:des   style   blog   http   color   strong   

原文地址:http://blog.csdn.net/fanerxiaoqinnian/article/details/37767517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!