Light oj 1033 - Generating Palindromes(区间dp)
2015-09-07 15:00
447 查看
1033 - Generating Palindromes
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.
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.
PROBLEM SETTER: MD. KAMRUZZAMAN
SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)
题意:插入最少字符使得原串变成回文串
![]() | PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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)
题意:插入最少字符使得原串变成回文串
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-8 typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define N 105 int len; char c ; int dp ; void DP() { int i,j; memset(dp,0,sizeof(dp)); for(int i=len-1;i>=0;i--) for(int j=i+1;j<len;j++) { dp[i][j]=min(dp[i][j-1],dp[i+1][j])+1; if(c[i]==c[j]) dp[i][j]=min(dp[i+1][j-1],dp[i][j]); } } int main() { int i,j,t,ca=0; scanf("%d",&t); while(t--) { scanf("%s",c); len=strlen(c); DP(); printf("Case %d: %d\n",++ca,dp[0][len-1]); } return 0; }
相关文章推荐
- HDU 3625 Examining the Rooms(第一类Stirling数)
- C++中随机函数rand()和srand()的用法
- ios开发系列之根据字典创建模型
- EntityFramework常用查询
- Hive性能调优
- 送给和我一样曾经浮躁过的PHP程序猿
- UltraEdit UEStudio 恢复默认窗口设置
- 慕课网上学习JS的笔记
- 从UIImage的矩阵变换看矩阵运算的原理
- 带Header与Footer的RecyclerViews
- iOS小结(二)navigation
- eclipse出现的一个问题
- 二级指针
- JB2上如何实现按power键亮屏的时候,能把触摸板上的home/menu/back虚拟按键的背光点亮?
- ext2文件系统实验目录
- HDFS和Hbase误删数据恢复
- Maven 入门教程 之 Myeclipse 安装Maven插件
- 数据结构实践项目——顺序表
- LeetCode_OJ【5】Longest Palindromic Substring
- js字符串转换成数字,数字转换成字符串