设为首页 加入收藏

TOP

UVA 10453 - Make Palindrome(区间DP)
2015-07-20 17:34:10 来源: 作者: 【 】 浏览:1
Tags:UVA 10453 Make Palindrome 区间

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

区间DP,枚举区间长度。

1:str[i]==str[j] dp[i][j]=dp[i+1][j-1]+1.

2:dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        typedef long long LL; using namespace std; const int maxn=1100; char str[maxn]; int dp[maxn][maxn]; int len; void output(int i,int j) { // cout<<"1111 "<
       
        j) return ; if(i==j) { printf("%c",str[i]); return ; } if(str[i]==str[j]) { printf("%c",str[i]); output(i+1,j-1); printf("%c",str[i]); } else if(dp[i][j]==dp[i+1][j]+1) { printf("%c",str[i]); output(i+1,j); printf("%c",str[i]); } else { printf("%c",str[j]); output(i,j-1); printf("%c",str[j]); } } int solve() { memset(dp,0,sizeof(dp)); for(int l=2;l<=len;l++) { for(int i=0;i+l-1
        
         

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BZOJ 1040 ZJOI2008 骑士 树形DP 下一篇Mac环境 go语言之入门HelloWorld

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)