zoukankan      html  css  js  c++  java
  • 10453 Make Palindrome (dp)

    Problem A

    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

    abcdaaaaabcaababababaabababapqrsabcdpqrs

    Sample Output

    3 abcdcba0 aaaa2 abcba1 baab0 abababaabababa9 pqrsabcdpqrqpdcbasrqp

    题意:给定字符串。可以在任意位置增添字符,求最少步骤生成回文串。以及生成的串

    思路:

    和这题一样。http://blog.csdn.net/accelerator_/article/details/11542037

    多开一个vis数组记录状态转移方式便于输出。

    代码:

    #include <stdio.h>
    #include <string.h>
    const int MAXN = 1005;
    
    char sb[MAXN];
    int dp[MAXN][MAXN], vis[MAXN][MAXN], n, i, j;
    
    void print(int i, int j) {
    	if (i == j) {
    		printf("%c", sb[i]);
    		return;
    	}
    	if (i > j)
    		return;
    	if (vis[i][j] == -1) {
    		printf("%c", sb[i]);
    		print(i + 1, j - 1);
    		printf("%c", sb[j]);
    	}
    	else if (vis[i][j] == 0) {
    		printf("%c", sb[i]);
    		print(i + 1, j);
    		printf("%c", sb[i]);
    	}
    	else if (vis[i][j] == 1) {
    		printf("%c", sb[j]);
    		print(i, j - 1);
    		printf("%c", sb[j]);
    	}
    }
    
    int main() {
    	while (gets(sb) != NULL) {
    		n = strlen(sb);
    		for (i = n - 1; i >= 0; i --) {
    			for (j = i + 1; j < n; j ++) {
    				if (sb[i] == sb[j]) {
    					dp[i][j] = dp[i + 1][j - 1];
    					vis[i][j] = -1;
    				}
    				else {
    					if (dp[i + 1][j] < dp[i][j - 1]) {
    						dp[i][j] = dp[i + 1][j] + 1;
    						vis[i][j] = 0;
    					}
    					else {
    						dp[i][j] = dp[i][j - 1] + 1;
    						vis[i][j] = 1;
    					}
    				}
    			}
    		}
    		printf("%d ", dp[0][n - 1]);
    		print(0, n - 1);
    		printf("
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    反思二
    安装Electron时卡在install.js不动的解决方案
    解决npm 下载速度慢的问题
    覆盖第三方jar包中的某一个类。妙!!
    关于拦截器是用注解方便,还是用配置文件写死方便的总结。
    yapi 启动后,老是自动关闭的问题。
    BaseResponse公共响应类,与我的设计一模一样,靠、ApiResponse
    HashMap 的 7 种遍历方式与性能分析!(强烈推荐)、forEach
    Jackson objectMapper.readValue 方法 详解
    yapi tag的问题,暂时只保留一个tag
  • 原文地址:https://www.cnblogs.com/james1207/p/3327364.html
Copyright © 2011-2022 走看看