【原】 POJ 1159 Palindrome 回文 动态规划 解题报告

http://poj.org/problem?id=1159


方法1:
c[i][j]表示:s[i...j]子字符串中最少需要插入多少个字符形成回文
c[1][n]为最终结果
初始值:
c[i][i] = 0
c[i][j] = 0,if i>j
递归式:
c[i][j] = c[i+1][j-1],if s[i]==s[j]
        = min{ c[i+1][j], c[i][j-1] } +1,otherwise

方法2:
len(s)-LCS(s,~s)即为最少插入数,~s为s的逆串
LCS(s,~s)即为原串中可以形成回文的字符的个数,len(s)-LCS(s,~s)为原串中不能形成回文的个数,
因此也是需要插入字符构成回文的最少字符数
可使用滚动数组使得空间需求从O(n*n)降到O(n)

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

   1: const int N = 5010 ;
   2:  
   3: char s[N] ;
   4: int c[N][N]={0} ;
   5: int n ;
   6:  
   7: //方法1
   8: //递归+记忆化
   9: int DP( int i, int j )
  10: {
  11:     if( i==j )
  12:         return 0 ;
  13:     if( i>j )
  14:         return 0 ;
  15:     if( c[i][j]!=0 )
  16:         return c[i][j] ;
  17:  
  18:     if( s[i]==s[j] )
  19:         c[i][j] = DP(i+1,j-1) ;
  20:     else
  21:         c[i][j] = std::min( DP(i+1,j), DP(i,j-1) ) +1 ;
  22:  
  23:     return c[i][j] ;        
  24: }
  25:  
  26: void run1159()
  27: {
  28:     scanf( "%d", &n ) ;
  29:     scanf( "\n%s", s+1 ) ;
  30:  
  31:     printf( "%d\n", DP(1,n) ) ;
  32: }
  33:  
  34: //=========================================================
  35:  
  36: //滚动数组用于DP
  37: int c1[2][N]={0} ;
  38:  
  39: //方法2
  40: void run1159_2()
  41: {
  42:     int i,j ;
  43:     char s1[N] ;
  44:  
  45:     scanf( "%d", &n ) ;
  46:     scanf( "\n%s", s+1 ) ;
  47:  
  48:     for( i=1 ; i<=n ; ++i )
  49:         s1[n+1-i] = s[i] ;
  50:  
  51:     for( i=1 ; i<=n ; ++i )
  52:     {
  53:         for( j=1 ; j<=n ; ++j )
  54:         {
  55:             if( s[i]==s1[j] )
  56:                 c1[i%2][j] = c1[(i-1)%2][j-1]+1 ;  //滚动数组
  57:             else
  58:                 c1[i%2][j] = std::max( c1[(i-1)%2][j] , c1[i%2][j-1] ) ;
  59:         }
  60:     }
  61:  
  62:     printf( "%d", n-c1[n%2][n] ) ;
  63: }

如果您满意我的博客,请点击“订阅Allen Sun的技术博客”即可订阅,谢谢:)

原创文章属于Allen Sun
欢迎转载,但请注明文章作者Allen Sun和链接
原文地址:https://www.cnblogs.com/allensun/p/1870052.html