【Lijhtoj 1033 Generating Palindromes +最大公共子序列】
来源:互联网 发布:linux shell脚本攻略 编辑:程序博客网 时间:2024/05/21 22:13
Generating Palindromes
Description
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 only allowed to insert characters at any position of the string.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains a string of lowercase letters denoting the string for which we want to generate a palindrome. You may safely assume that the length of the string will be positive and no more than 100.
Output
For each case, print the case number and the minimum number of characters required to make string to a palindrome.
Sample Input
6
abcd
aaaa
abc
aab
abababaabababa
pqrsabcdpqrs
Sample Output
Case 1: 3
Case 2: 0
Case 3: 2
Case 4: 1
Case 5: 0
Case 6: 9
实际就是找最大公共子序列裸体:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;char st[110];int dp[110][110];int main(){ int T,nl = 0,i,j,pl; scanf("%d",&T); while(T--) { scanf("%s",st + 1); pl = strlen(st + 1); memset(dp,0,sizeof(dp)); for(i = pl - 1 ; i >= 1 ; i--) for(j = i + 1 ; j <= pl ; j++) { if(st[i] == st[j]) dp[i][j] = dp[i + 1][j - 1]; else dp[i][j] = min(dp[i + 1][j],dp[i][j - 1]) + 1 ; } printf("Case %d: %d\n",++nl,dp[1][pl]); } return 0;}
- 【Lijhtoj 1033 Generating Palindromes +最大公共子序列】
- lightoj - 1033 - Generating Palindromes - dp/ 最长公共子序列lcs
- 最大公共子序列
- 最大公共子序列
- 最大公共子序列
- 最大公共子序列
- 最大公共子序列
- 最大公共子序列
- 最大公共子序列
- 最大公共子序列
- 总结---最大公共子序列
- hdu1159 最大公共子序列
- LCS最大公共子序列
- 最大公共子序列LCS
- 最大公共子序列模板
- DP最大公共子序列
- LCS 最大公共子序列
- LightOJ 1033 - Generating Palindromes
- spring什么sh时候实例化bean
- 触摸屏getevent正常报数据,Android却无触点的bug
- 第一章 基本概念
- Android vector标签 PathData 画图超详解
- session详解
- 【Lijhtoj 1033 Generating Palindromes +最大公共子序列】
- 创建命名空间的几种方法
- 第二章 类与对象
- Java中的垃圾回收
- 算法 Tricks(五)—— 二进制逻辑运算
- C++STL:erase
- 利用MSBuild制作msf免杀的后门
- Java 编程要点之并发(Concurrency)详解
- JLink V8 破解(Jlink v6.10a + Win8.1 + 虚拟机WinXP )