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. centos安装后的个人工具

    1.安装vim工具 yum -y install vim 安装完成后在家目录下新建一个.vimrc的配置文件.辅助vim软件功能. set number " 显示行号 set cursorl ...

  2. 学习java的第八天

    一.今日收获 1.学习完全学习手册上2.3转义字符与2.4运算符两节 二.今日难题 1.没有什么难理解的问题 三.明日目标 1.哔哩哔哩教学视频 2.Java学习手册

  3. MapReduce02 序列化

    目录 MapReduce 序列化 概述 自定义序列化 常用数据序列化类型 int与IntWritable转化 Text与String 序列化读写方法 自定义bean对象实现序列化接口(Writable ...

  4. ace

    ace An ace is a playing card, die or domino with a single pip. In the standard French deck, an ace h ...

  5. Scala(八)【面向对象总结】

    面向对象总结 面向对象 1.scala包 1.声明包 1.在文件第一行通过package 包名 2.package 包名{ .... } 第二种方法,包名只能在target目录才能看到 2.导入包 1 ...

  6. 大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户

    1. 读取数据库的形式创建DataFrame DataFrameFromJDBC object DataFrameFromJDBC { def main(args: Array[String]): U ...

  7. 零基础学习java------32---------css,javascript,jQuery

    一. CSS简单了解 需要掌握: 概念见day11中的课堂笔记 css:修饰html标签的样式 1.每个元素有一个style属性,其形式为:style="属性:值:属性:值...." ...

  8. css系列,选择器权重计算方式

    CSS选择器分基本选择器(元素选择器,类选择器,通配符选择器,ID选择器,关系选择器), 属性选择器,伪类选择器,伪元素选择器,以及一些特殊选择器,如has,not等. 在CSS中,权重决定了哪些CS ...

  9. Linux下删除的文件如何恢复

    Linux下删除的文件如何恢复 参考自: [1]linux下误操作删除文件如何恢复 [2]Linux实现删除撤回的方法 以/home/test.txt为例 1.df -T 文件夹 找到当前文件所在磁盘 ...

  10. Templates and Default Arguments

    Default parameters for templates in C++: Like function default arguments, templates can also have de ...