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. 同一局域网,远程连接别人的Mysql数据库

    数据库:MySQL 工具: Navicat, 电脑A连接电脑B的数据库, 确保两部电脑都是在同一个局域网,都是连着同一个路由器,或者连接同一个WiFi, 如果不确定是否为同一个局域网,可以打开cmd, ...

  2. c#跳转

    Response.Redirect(EditUrl("MEUID", lblMEUID.Text, "Page2", "PageOneMK" ...

  3. 学习java 7.4

     学习内容:遍历字符串要点:for(int i = 0;i < line.length();i++) { System.out.println(line.chatAt(i)); } 字符串拼接: ...

  4. go 代理

    环境变量中设置 #GO111MODULE=auto GOPROXY=https://goproxy.io 如果不第一次,则在命令行设置 go env -w GO111MODULE=on go env ...

  5. CentOS 6.4 下 Python 2.6 升级到 2.7

    一开始有这个需求,是因为用 YaH3C 替代 iNode 进行校园网认证时,CentOS 6.4下一直编译错误,提示找不到 Python 的某个模块,百度了一下,此模块是在 Python2.7 以上才 ...

  6. 对于HTML和XML的理解

    1.什么是HTML??? HTML就是 超文本标记语言(超文本含义:超过文本 --图片 .视频.音频. 超链接) 2.HTML作用 把网页的信息格式化的展现,对网页信息进行规范化展示 连接(https ...

  7. Spring Boot项目的不同启动方式

    方式一: 直接通过IntelliJ IDEA启动,直接执行Spring Boot项目的main()方法. 方法二: 将项目打包成jar包,首先需要在pom.xml文件的根节点下添加如下配置: < ...

  8. 一个超好用的 Python 标准库,彻底玩透路径操作

    pathlib 学习 Python 时,尤其是在进行文件操作和数据处理时,经常会处理路径问题.最常用和常见的是 os.path 模块,它将路径当做字符串进行处理,如果使用不当可能导致难以察觉的错误,而 ...

  9. Redis集群环境各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的解决方式

    总结/朱季谦 在搭建Redis5.x版本的集群环境曾出现各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的情况,故而把解决方式记录下来. 在以下 ...

  10. 06 - Vue3 UI Framework - Dialog 组件

    做完按钮之后,我们应该了解了遮罩层的概念,接下来我们来做 Dialog 组件! 返回阅读列表点击 这里 需求分析 默认是不可见的,在用户触发某个动作后变为可见 自带白板卡片,分为上中下三个区域,分别放 ...