Uva--10453(动规)

2014-08-05 00:15:23

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

abcd aaaa abc aab abababaabababa pqrsabcdpqrs

Sample Output

3 abcdcba 0 aaaa 2 abcba 1 baab 0 abababaabababa 9 pqrsabcdpqrqpdcbasrqp

Author : Md. Kamruzzaman

The Real Programmers' Contest-2

 

思路:经典回文DP,唯一要注意的是输出要回溯,不然容易错。

 1 /*************************************************************************
 2  
 3     > File Name: i.cpp
 4     > Author: Nature
 5     > Mail: 564374850@qq.com 
 6     > Created Time: Mon 04 Aug 2014 01:50:06 PM CST
 7 ************************************************************************/
 8 
 9 #include <cstdio>
10 #include <cstring>
11 #include <cstdlib>
12 #include <cmath>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 
17 char s[1005];
18 int dp[1005][1005];
19 int len;
20 
21 void Print(int x,int y){
22     if(x > y)
23         return;
24     if(x == y){
25         printf("%c",s[x]);
26         return;
27     }
28     if(s[x] == s[y]){
29         printf("%c",s[x]);
30         Print(x + 1,y - 1);
31         printf("%c",s[y]);
32     }
33     else{
34         if(dp[x][y] == dp[x + 1][y] + 1){
35             printf("%c",s[x]);
36             Print(x + 1,y);
37             printf("%c",s[x]);
38         }
39         else{
40             printf("%c",s[y]);
41             Print(x,y - 1);
42             printf("%c",s[y]);
43         }
44     }
45 }
46 
47 int main(){
48     while(scanf("%s",s + 1) == 1){
49         len = strlen(s + 1); 
50         memset(dp,0,sizeof(dp));
51         for(int i = len; i >= 1; --i){
52             for(int j = i + 1; j <= len; ++j){
53                 if(s[i] != s[j]){
54                     if(dp[i + 1][j] < dp[i][j - 1]){
55                         dp[i][j] = dp[i + 1][j] + 1;
56                     }
57                     else{
58                         dp[i][j] = dp[i][j - 1] + 1;
59                     }
60                 }
61                 else
62                     dp[i][j] = dp[i + 1][j - 1];
63             }
64         }
65         printf("%d ",dp[1][len]);
66         Print(1,len);
67         printf("\n");
68     }
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3891343.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值