light oj 1033 - Generating Palindromes 【LCS】
来源:互联网 发布:剑三霸刀正太捏脸数据 编辑:程序博客网 时间:2024/03/29 01:42
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)
题意:
给你一个字符串,问最少添加多少个字符,使原字符串变成回文串
思路:
回文串的性质是倒过来两者一样, 那么有这个性质可知, 设原始字符串为a,原始字符串颠倒过来的字符串为b, 那么求a和b的最长公共子序列, 字符串a的长度减去最长公共自序列的长度不就是最少添加的字符!
#include <map>#include <set>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <iostream>#include <stack>#include <cmath>#include <string>#include <vector>#include <cstdlib>//#include <bits/stdc++.h>//#define LOACL#define space " "using namespace std;//typedef long long LL;//typedef __int64 Int;typedef pair<int, int> paii;const int INF = 0x3f3f3f3f;const double ESP = 1e-5;const double PI = acos(-1.0);const int MOD = 1e9 + 7;const int MAXN = 1e2 + 10;int dp[MAXN][MAXN];char str1[MAXN], str2[MAXN];int main(int argc, char* argv[]) {int T;scanf("%d", &T);int Kcase = 0; while (T--) {scanf("%s", str1);int len = strlen(str1);memset(dp, 0, sizeof(dp));memset(str2, 0, sizeof(str2));for (int i = len - 1; i >= 0; i--) {str2[len - i - 1] = str1[i];}for (int i = 1; i <= len; i++) {for (int j = 1; j <= len; j++) {if (str1[i-1] == str2[j-1]) { dp[i][j] = dp[i-1][j-1]+1; } else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);}}printf("Case %d: %d\n", ++Kcase, len - dp[len][len]); } 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
- VMWare虚拟机提示:“锁定文件失败,打不开磁盘或快照所依赖的磁盘”的解决方法
- 从文本中随机选择百万行
- linux基础命令操作
- android入门详解
- 将身份证号后六位替换成*方法
- light oj 1033 - Generating Palindromes 【LCS】
- 大数据系列修炼-Scala课程64
- 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- Remoting和Webservice的区别
- Ubuntu 安装 Oracle JDK
- 链表的操作
- vi\vim -关于清空文件内容快捷命令
- JAVA之Annotation (一)
- 【Linux 系统编程】shell 脚本基础学习之函数(五)