Light OJ:1033 Generating Palindromes(LCS+回文字符串)
来源:互联网 发布:网络用语 盐 编辑:程序博客网 时间:2024/03/29 00:05
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
Output for Sample Input
6
abcd
aaaa
abc
aab
abababaabababa
pqrsabcdpqrs
Case 1: 3
Case 2: 0
Case 3: 2
Case 4: 1
Case 5: 0
Case 6: 9
SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)
题目大意:给你一个字符串,问你将其变成一个回文字符串最少需要添加多少个字符。
解题思路:答案=所给字符串的长度-所给字符串与它的反转字符串的LCS长度。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std; int main(){int t;scanf("%d",&t);char s1[110];char s2[110];int dp[110][110];int kcase=1;while(t--){memset(dp,0,sizeof(dp));scanf("%s",s1);int size1=strlen(s1);int size2=0;for(int i=size1-1;i>=0;i--)//反转字符串 {s2[size2]=s1[i];size2++;}s2[size2]='\0';//别忘了这个 int ans=0;for(int i=1;i<=size1;i++)//求LCS长度 {for(int j=1;j<=size2;j++){if(s1[i-1]==s2[j-1]){dp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}ans=max(ans,dp[i][j]);} }printf("Case %d: %d\n",kcase++,size1-ans);}return 0;}
- Light OJ:1033 Generating Palindromes(LCS+回文字符串)
- light oj 1033 - Generating Palindromes (LCS)
- light oj 1033 - Generating Palindromes 【LCS】
- Light OJ 1033 - Generating Palindromes
- light oj 1033 - Generating Palindromes (区间dp)
- light oj 1033 - Generating Palindromes (区间DP)
- Light OJ 1033 Generating Palindromes (最长回文子串 区间DP)
- Light oj 1033 - Generating Palindromes(区间dp)
- lightoj-1033-Generating Palindromes【思维】【LCS】
- lightoj1033 - Generating Palindromes (LCS)
- lightoj - 1033 - Generating Palindromes - dp/ 最长公共子序列lcs
- 思维 lcs lightoj1033(Generating Palindromes)
- LightOJ 1033 - Generating Palindromes
- LightOJ 1033 - Generating Palindromes
- LightOJ 1033 Generating Palindromes
- LightOJ-1033-Generating Palindromes
- Light OJ 1258 Making Huge Palindromes 末尾添加最少字符变回文串
- LIGHT OJ 1258 - Making Huge Palindromes 【包含最后一位字符的最长回文串()】
- 适配器adapter
- 42.Maximum Subarray II-最大子数组 II(中等题)
- Bootstrap结合BootstrapTable的使用
- Mac系统终端命令行不执行命令 总出现command not found解决方法
- Day30网络
- Light OJ:1033 Generating Palindromes(LCS+回文字符串)
- Codeforces Round #363 (Div. 2) C. Vacations 贪心+dp
- JTAG JLink ULINK ST-LINK仿真器区别
- hive sql层面优化
- vs2015上使用github进行版本控制
- 浅谈this
- 一张图看明白CAS单点登录原理
- 用flask开发个人博客(14)—— flask中本地化时间的引用
- 关于sizeof和strlen的区别