Palindrome 簡單(dp)poj1159

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

以下代碼Time超出範圍

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
    int n,i,j,k,m,p[5005],d[5005],max;
    char a[5005],c[5005];
    while(cin>>n)
    {
        cin>>a;
        j=0;
        memset(p,-1,sizeof(p));
        memset(d,0,sizeof(d));
        for(i=n-1;i>=0;i--)
        {
            c[i]=a[j];
            j++;
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(a[i]==c[j])
                { max=-1;
                    //cout<<a[i]<<c[j]<<endl;
                   for(k=0;k<=i;k++)
                   {
                      // cout<<j<<"j"<<k<<"k"<<p[k]<<endl;
                      if(j>p[k]&&max<d[k])
                      {

                          max=d[k];
                            m=j;
                      }
                   }
                   if(d[i+1]<max+1||(d[i+1]==max+1&&m<p[i+1]))
                   {
                       d[i+1]=max+1;
                        p[i+1]=m;
                   }

                }
                //cout<<d[i+1]<<p[i+1]<<endl;
            }
        }
        max=0;
        for(i=0;i<=n;i++)
        {
            if(max<d[i])
            {
                max=d[i];
            }
        }
        cout<<n-max<<endl;
    }
    return 0;
}

以下代碼正確

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
    int n,i,j,m,dp[2][5005];
    char a[5005];
    while(cin>>n)
    {
        cin>>a;
        j=0;
        memset(dp,0,sizeof(dp));
        for(i=n-1;i>=0;i--)
        {
            for(j=i+1;j<=n;j++)
            {
                if(a[i]==a[j])
                {
                    dp[i%2][j]=dp[(i+1)%2][j-1];
                }
                else
                dp[i%2][j]=min(dp[(i+1)%2][j],dp[i%2][j-1])+1;
            }
        }

        cout<<dp[0][n-1]<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章