LightOJ 1033  Generating Palindromes(dp)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/A

题目:

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),从起点和终点开始往中间进行dp

状态转移方程:

当s[i]==s[j]时,dp[i][j]=dp[i+1][j-1];
当s[i]!=s[j]时,dp[i][j]=min(dp[i+1][j],dp[i][j-1])+1;

代码:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; char s[];
int dp[][]; int min(int a,int b)//输出最小值
{
return a>b?b:a;
} int main()
{
int t;
int n,m=;
scanf("%d",&t);
while(t--)
{
scanf("%s",s+);//从数组下标为1的位置开始
int n=strlen(s+);//输入字符串的长度
memset(dp,,sizeof(dp));
for(int i=n;i>=;i--)//从两边开始搜索
for(int j=i+;j<=n;j++)
{
if(s[i]==s[j]) //s[i]与s[j]相等,搜索下一个
dp[i][j]=dp[i+][j-];
else //s[i]与s[j]不相等,取一个最小值再加1
dp[i][j]=min(dp[i+][j],dp[i][j-])+;
}
printf("Case %d: %d\n",m++,dp[][n]);
}
return ;
}

LightOJ 1033 Generating Palindromes(dp)的更多相关文章

  1. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  2. 【ARC064-F】【XSY2575】Rotated Palindromes(DP)(字符串)

    Description 然而,由于小C沉迷于制作游戏,他完全忘记了自己作为国家集训队的一员,还有156道作业题等他完成.还有一天作业就要截止了,而他一题还没有做.于是他赶紧挑了一道看起来最简单的题: ...

  3. URAL1635. Mnemonics and Palindromes(DP)

    链接 先初始化一下所有的回文串 再O(n*n)DP 输出路径dfs 刚开始存所有回文 ME了 后来发现用不着 改了改了OK了 数据还挺强 #include <iostream> #incl ...

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

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

  5. Ural 1635 Mnemonics and Palindromes(DP)

    题目地址:space=1&num=1635">Ural 1635 又是输出路径的DP...连着做了好多个了. . 状态转移还是挺简单的.要先预处理出来全部的回文串,tag[i] ...

  6. 【UVa】Partitioning by Palindromes(dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=sh ...

  7. 1033 - Generating Palindromes

    1033 - Generating Palindromes    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...

  8. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  9. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

随机推荐

  1. 转: html5 history api详解~很好的文章

    从Ajax翻页的问题说起 请想象你正在看一个视频下面的评论,在翻到十几页的时候,你发现一个写得稍长,但非常有趣的评论.正当你想要停下滚轮细看的时候,手残按到了F5.然后,页面刷新了,评论又回到了第一页 ...

  2. Developing your first FNC custom control

    Friday, May 13, 2016 Some weeks ago, we released the TMS FNC UI Pack, a set of Framework Neutral Com ...

  3. 窗口嵌入到另一个窗口(VC和QT都有)

    1.用vc新建一个dialog1工程.属性默认. 2.insert一个dialog2,改为child. 3.在dialog1中包含dialog2头文件,在一个按钮事件中显示dialog2: Cdial ...

  4. Codeforces Round#344

    A题意思是,给出两个数列,求一个区间,使第一个数列的区间或和第二个数列的区间或的和最大,输出最大和 很显然,或运算会使得答案越运算越大.所以,直接全部或起来,相加就是答案. = =打cf的时候自动脑补 ...

  5. gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典

    gravitas是什么意思_gravitas在线翻译_英语_读音_用法_例句_海词词典 gravitas

  6. HTML DOM 创建与修改

    修改 HTML 元素 修改 HTML DOM 意味着许多不同的方面: 改变 HTML 内容 改变 CSS 样式 改变 HTML 属性 创建新的 HTML 元素 删除已有的 HTML 元素 改变事件(处 ...

  7. PHP中递归函数的一个常见逻辑问题

    首先.我们得知道递归函数是什么东西.通俗来讲也就是自己调用自己本身的函数. 如今须要设计一段代码.解决1到10叠加的问题. 代码A: <?php //递归函数 $num=10; function ...

  8. js页面loading加载

    <html>     <head>     <title>页面正在载入</title>       <script  language=" ...

  9. 关于MATLAB中any和all的个人理解

    any: any=1,有一个元素是1,即可: any=0,全部元素为0,即可. all: all=1,全部元素是1,即可: all=0,有一个元素是0,即可. ALL True if all elem ...

  10. C++ STL中map存储方式——SAP电面(4)

    map存储方式  一般是平衡二叉树 红黑树