Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

 

Description

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

6

abcd

aaaa

abc

aab

abababaabababa

pqrsabcdpqrs

Sample Output

Case 1: 3

Case 2: 0

Case 3: 2

Case 4: 1

Case 5: 0

Case 6: 9

题解:通过插入把给的字符串变为回文串,求出最小操作数。

状态转移:dp[i][j] = min(dp[i+1][j],dp[i][j+1])+1; (s[i]!=s[j])

dp[i][j] = dp[i+1][j-1];  (s[i]==s[j])

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int t,k,dp[][];
char s[];
int main()
{
cin>>t;
k=;
while (t--)
{
cin >> s;
int len = strlen(s);
memset(dp,,sizeof(dp));
for (int i = len - ; i >= ; i--)
for (int j = i + ; j < len; j++)
{
if (s[i] == s[j])
dp[i][j] = dp[i+][j-];
else
dp[i][j] = min(dp[i+][j],dp[i][j-]) + ;
}
cout<<"Case "<<k++<<": "<<dp[][len-]<<endl;
}
return ;
}

周赛A题的更多相关文章

  1. CSDN 轻松周赛赛题:能否被8整除

    轻松周赛赛题:能否被8整除 题目详情 给定一个非负整数,问能否重排它的全部数字,使得重排后的数能被8整除. 输入格式: 多组数据,每组数据是一个非负整数.非负整数的位数不超过10000位. 输出格式 ...

  2. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

  3. 周赛F题 POJ 1458(最长公共子序列)

    F - F Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Description ...

  4. 周赛D题

    D - D Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  5. leetcode 第184场周赛第一题(数组中的字符串匹配)

    一.函数的运用 1,strstr(a,b); 判断b是否为a的子串,如果是,返回从b的开头开始到a的结尾 如“abcdefgh” “de” 返回“defgh”: 如果不是子串,返回NULL: 2,me ...

  6. Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)

    这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式   给你一个字符串 date ,它的格式为 Day Month Yea ...

  7. 周赛C题 LightOJ 1047 (DP)

    C - C Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description Th ...

  8. LeetCode | 力扣周赛C题 5370. 设计地铁系统

    请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: checkIn(int id, string stationName, int t) 编号为 id 的乘客在 t 时刻进 ...

  9. LeetCode周赛#208

    本周周赛的题面风格与以往不太一样,但不要被吓着,读懂题意跟着模拟,其实会发现并不会难到哪里去. 1599. 经营摩天轮的最大利润 #模拟 题目链接 题意 摩天轮\(4\)个座舱,每个座舱最多可容纳\( ...

随机推荐

  1. HDU 1203 I NEED A OFFER! 01背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 解题思路:简单的01背包,用dp[i]表示花费不超过i时的最大可能性 状态转移方程 dp[i]= ...

  2. Best Reward HDU 3613(回文子串Manacher)

    题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值.   分析:首先的明白这个最大价值有 ...

  3. JDBC 之 事务

    1.概念:事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. 2.数据库开启事务的命令dtl: Start transaction开启事务 Rollback回滚事务(撤销) ...

  4. JSP中的路径

    我的原则 所有路径一律使用绝对路径,就是以"/"开头的或者带host的路径.形如: /imgs/1.jpg <%= request.getContextPath()%> ...

  5. PHP安全编程:会话数据注入 比会话劫持更强大的攻击(转)

    一个与会话暴露类似的问题是会话注入.此类攻击是基于你的WEB服务器除了对会话存储目录有读取权限外,还有写入权限.因此,存在着编写一段允许其他用户添加,编辑或删除会话的脚本的可能.下例显示了一个允许用户 ...

  6. 再回首,Java温故知新(六):Java基础之变量

    变量在所有的编程语言中都有,属于最最基础的部分,学习这部分基本不分语言,变量可分为成员变量和局部变量 Java中变量的声明格式如下,修饰符如果不加,默认是default,关于修饰符,我们会在后续讲到, ...

  7. [转] openjdk和jdk的区别与联系

    使用过LINUX的人都应该知道,在大多数LINUX发行版本里,内置或者通过软件源安装JDK的话,都是安装的openjdk,那么到底什么是openjdk,它与sun jdk有什么关系和区别呢? 历史上的 ...

  8. getopt 分析命令行参数 -n -t 1

    在Linux中,我们常常用到 ls -l 等等之类带有选项项的命令,下面,让我们用C++来实现该类似的命令. 在实现之前,首先,我们来介绍一下一个重要函数:getopt() 表头文件 #include ...

  9. 异步编程中使用帮助类来实现Thread.Start()的示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. Activity 【生命周期】

    不同情况下的回调 我们打开应用时先后调用了onCreate()->onStart()->onResume 当我们按BACK键时,我们这个应用程序将结束,这时候我们将先后调用onPause( ...