light oj 1033 - Generating Palindromes (LCS)
来源:互联网 发布:网上打电话软件 编辑:程序博客网 时间:2024/04/26 14:17
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
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int dp[110][110];int main(){ int n,len,kcase=1; char str[110],s[110]; scanf("%d",&n); while(n--) { scanf("%s",str+1); len=strlen(str+1); int u=1; for(int i=len;i>=1;i--) s[u++]=str[i]; for(int i=0;i<=len;i++) dp[i][0]=0; for(int j=0;j<=len;j++) dp[0][j]=0; for(int i=1;i<=len;i++) { for(int j=1;j<=len;j++) { if(str[i]==s[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i][j-1],dp[i-1][j]); } } int ans=len-dp[len][len]; printf("Case %d: %d\n",kcase++,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)
- lightoj-1033-Generating Palindromes【思维】【LCS】
- Light OJ 1033 Generating Palindromes (最长回文子串 区间DP)
- 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 1159 - Batman LCS
- light oj An Easy LCS
- 真实的安卓公司面试题?希望帮助大家(二)
- javascript连=符号
- 第五周 项目二——链栈算法库
- Qt下载安装
- 学生会竞选新闻稿
- light oj 1033 - Generating Palindromes (LCS)
- Java-NowCoder-删除字符串中出现次数最少的字符
- LaTex 制作表格 合并行\multirow 合并列\multicolumn
- 【Java系列】(六)Java中常用的加密方法---单向加密算法
- 快速排序的python和c++
- PHP中的时间函数
- LeetCode 27. Remove Element
- hdu 1576 乘法逆元(入门)
- python 之版本控制