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. 升级automake和autoconf

    <pre name="code" class="html">zjtest7-redis:/root/soft/json-c-json-c-0.12- ...

  2. 第八届河南省赛B.最大岛屿(dfs)

    B.最大岛屿 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 30  Solved: 18 [Submit][Status][Web Board] De ...

  3. 收藏:左路Deep Learning+右路Knowledge Graph,谷歌引爆大数据

    发表于2013-01-18 11:35| 8827次阅读| 来源sina微博 条评论| 作者邓侃 数据分析智能算法机器学习大数据Google 摘要:文章来自邓侃的博客.数据革命迫在眉睫. 各大公司重兵 ...

  4. 使用COCOS2D-X JSB开发,在js页面中设置iOS键盘模式

    XYSDK.h void setKeyboardType(int type); XYSDK.cpp voidXYSDK::setKeyboardType(int type) { #if (CC_TAR ...

  5. Linux Shell 学习笔记 一 目录结构

    以Red Hat Enterprise Linux 各版本为例,RHEL中目录具体作用如下, /bin       存放普通用户使用的命令 /sbin     存放管理员可以执行的命令 /home   ...

  6. 【微信公众号】使用a标签打开链接显示空白

    window.location.href 改成 top.location.href

  7. 使用Xib添加自定义View

    1.新建Cocoa Touch Class以及UI View,2者同名 2.设置UI View的File's Owner——Custom Class为之前新建类 3.设置Xib中View与类关联 4. ...

  8. 如何最简单的优化MySql

    1.创建索引,一定要根据实际情况来创建,如果是连接表查询,如一个主帐号连接多个子帐号,可以考虑两个或三个以上的多索引: 2.合理利用时间排序,由于大多数表格用时间来排序,数据量相当大的时候,在时间列上 ...

  9. LeetCode Backpack

    Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this ...

  10. BEGIN_SINK_MAP(CMainDlg) SINK_ENTRY(IDC_EXPLORER1, ..。响应不到的

    </pre><pre name="code" class="cpp"> class CMainDlg : public CAxDialo ...