1033 - Generating Palindromes
Time Limit: 2 second(s) Memory Limit: 32 MB

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


PROBLEM SETTER: MD. KAMRUZZAMAN
SPECIAL THANKS: JANE ALAM JAN (MODIFIED DESCRIPTION, DATASET)
题意:让你补一些字母,使所给的串成为回文串,要求补的这些字母的个数最少。
思路:区间dp;
dp[i][j]表示,i,j这段成为回文的最少要添加的字母的个数。
状态转移方程:如果str[i]==str[j];那么dp[i][j]=dp[i+1][j-1];也就是在区间i+1,j-1;已经成为回文,那么两端再添加两个相同的字符依然是回文,
当str[i]!=str[j]的时候,那么在区间i+1,j-1;已经成为回文,那么考虑是在是添加一个字母和str[i]相同,还是和str[j]相同,dp[i][j]=min(dp[i+1][j],dp[i][j-])+1;
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<string.h>
6 #include<queue>
7 #include<stack>
8 #include<math.h>
9 using namespace std;
10 int dp[200][200];
11 char str[200];
12 char astr[200];
13 int main(void)
14 {
15 int i,j,k;
16 int s;
17 scanf("%d",&k);
18 for(s=1; s<=k; s++)
19 {
20 scanf("%s",str);
21 int l=strlen(str);
22 int u=0;
23 for(i=l-1; i>=0; i--)
24 {
25 astr[u++]=str[i];
26 }
27 for(i=0; i<200; i++)
28 {
29 for(j=0; j<200; j++)
30 {
31 dp[i][j]=1e9;
32 }
33 }
34 int maxx=0;
35 for(i=0; i<l; i++)
36 {
37 for(j=i; j>=0; j--)
38 {
39 if(i==j)
40 {
41 dp[j][i]=0;
42 }
43 else
44 {
45 if(str[i]==str[j])
46 {
47 dp[j][i]=dp[j+1][i-1];
48 if(j+1>i-1)
49 {
50 dp[j][i]=0;
51 }
52 }
53 else
54 {
55 dp[j][i]=min(dp[j][i-1],dp[j+1][i])+1;
56 }
57 }
58 }
59 }
60 printf("Case %d: %d\n",s,dp[0][l-1]);
61 }
62 return 0;
63 }

1033 - Generating Palindromes的更多相关文章

  1. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. Light OJ 1033 - Generating Palindromes(区间DP)

    题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串.   ============================================================= ...

  3. Generating Palindromes LightOJ - 1033

    Generating Palindromes LightOJ - 1033 题意:添加最少的字符使得给出的字符串成为回文串.输出添加的字符数. 方法:常规区间dp.ans[i][j]表示使得ans[i ...

  4. LightOJ1033 Generating Palindromes(区间DP/LCS)

    题目要计算一个字符串最少添加几个字符使其成为回文串. 一年多前,我LCS这道经典DP例题看得还一知半解时遇到一样的问题,http://acm.fafu.edu.cn/problem.php?id=10 ...

  5. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  6. dp百题大过关(第一场)

    好吧,这名字真是让我想起了某段被某教科书支配的历史.....各种DP题层出不穷,不过终于做完了orz 虽然各种手糊加乱搞,但还是要总结一下. T1 Monkey Banana Problem    这 ...

  7. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  8. 【hihoCoder】1033: 交错和

    初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...

  9. Codeforces 722D. Generating Sets

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. Portrait Photography Beginners Guide

    Please visit photoandtips稻糠亩 for more information. 六级/考研单词: vogue, derive, gorgeous, thereby, strict ...

  2. abuse

    abuse 近/反义词: ill-treat, maltreat, mistreat, misuse, prostitute, spoil; defame, disparage, malign, re ...

  3. Scala(二)【基本使用】

    一.变量和数据类型 1.变量 语法:val / var 变量名:变量类型 = 值 val name:String = "zhangsan" 注意 1.val定义的变量想到于java ...

  4. 大数据学习day15----第三阶段----scala03--------1.函数(“_”的使用, 函数和方法的区别)2. 数组和集合常用的方法(迭代器,并行集合) 3. 深度理解函数 4 练习(用java实现类似Scala函数式编程的功能(不能使用Lambda表达式))

    1. 函数 函数就是一个非常灵活的运算逻辑,可以灵活的将函数传入方法中,前提是方法中接收的是类型一致的函数类型 函数式编程的好处:想要做什么就调用相应的方法(fliter.map.groupBy.so ...

  5. Oracle——生成Awr报告

    Oracle--生成Awr报告 AWR的概念 Oracle数据库是一个使用量很多的数据库,关于Oracle数据库的性能.Oracle10g以后,Oracle提供了一个性能检测的工具:AWR(Autom ...

  6. entfrm-boot开发平台一览【entfrm开源模块化无代码开发平台】

    介绍 entfrm-boot是一个以模块化为核心的无代码开发平台,能够让中小企业快速从零搭建自己的开发平台:开箱即用,可插拔可自由组合:以模块化的方式,最大化的代码复用,避免重复开发:无代码可视化开发 ...

  7. linux环境下安装jdk,tomcat

    一.安装tomcat 1.使用docker安装(你得linux服务器上已经安装了docker) 1)执行命令: docker search tomcat; 2)选择第一个镜像进行下载,执行命令:doc ...

  8. 【Java多线程】Java 原子操作类API(以AtomicInteger为例)

    1.java.util.concurrent.atomic 的包里有AtomicBoolean, AtomicInteger,AtomicLong,AtomicLongArray, AtomicRef ...

  9. 3.使用Spring Data ElasticSearch操作ElasticSearch(5.6.8版本)

    1.引入maven坐标 <!--spring-data-elasticsearch--><dependency> <groupId>org.springframew ...

  10. 莫烦python教程学习笔记——总结篇

    一.机器学习算法分类: 监督学习:提供数据和数据分类标签.--分类.回归 非监督学习:只提供数据,不提供标签. 半监督学习 强化学习:尝试各种手段,自己去适应环境和规则.总结经验利用反馈,不断提高算法 ...