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. HttpContext.Current.Cache 和 HttpRuntime.Cache 区别

    原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...

  2. 手把手教你把VIM改成一个集成开发环境

    转载自:http://blog.csdn.net/wooin/article/details/1858917 在原文基础上经过自己验证,修改和完善 OS:Fedora19 VIM:VIM7.4 一.写 ...

  3. 《University Calculus》-chaper8-无穷序列和无穷级数-等比级数

    前言:其实无穷序列和无穷级数和数列{an}以及我们接触微积分就给出的极限概念lim有着紧密的联系,它对于我们在具体的问题当中进行建模和数据分析有着非常重要的作用. 无穷序列: 最简单的一种说法,就是一 ...

  4. poj1611 并查集

    题目链接:http://poj.org/problem?id=1611 #include <cstdio> #include <cmath> #include <algo ...

  5. poj 3903 最长上升子序列 Stock Exchange

    题目链接:http://poj.org/problem?id=3903 #include <cstdio> #include <cmath> #include <algo ...

  6. [置顶] SpecDD系列:6个确保您执行“充分”QA测试的技巧

    确保团队执行 “足够的” 测试覆盖面是非常困难的,尤其是对敏捷开发团队来说.对于初学者而言,一个开发Sprint中要完成多少的质量保证工作才够呢?我们知道,敏捷的标准是在开发Sprint结束的时候要完 ...

  7. SpringMVC 学习笔记(一) Hello World

    springMVC概述: Spring MVC 是眼下最主流的MVC 框架之中的一个 Spring MVC 通过一套 MVC 注解.让 POJO 成为处理请 求的控制器,而无须实现不论什么接口. 支持 ...

  8. Windows下配置Nginx使之支持PHP(转)

    平台描述:Windows下,使用PHP套件 xampp,因为是测试玩,所以没在服务器 Linux 环境中配置. 1. 首先,将 nginx.conf 中的 PHP 配置注释去掉. 01 # pass ...

  9. java多态 -- 猫狗案列

    我们用猫狗案例来表明在java中使用多态的好处: class Animal{ public Animal(){} public void eat(){ System.out.println(" ...

  10. NPOI从数据库中导出数据到Excel

    首先要添加NPOI.dll程序集 https://yunpan.cn/cMeSTELJSXmJJ  访问密码 8d83 把里面的程序集都添加到引用里 下面的代码是从数据库导出到Excel { //pa ...