LightOJ 1033 Generating Palindromes(dp)
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)的更多相关文章
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- 【ARC064-F】【XSY2575】Rotated Palindromes(DP)(字符串)
Description 然而,由于小C沉迷于制作游戏,他完全忘记了自己作为国家集训队的一员,还有156道作业题等他完成.还有一天作业就要截止了,而他一题还没有做.于是他赶紧挑了一道看起来最简单的题: ...
- URAL1635. Mnemonics and Palindromes(DP)
链接 先初始化一下所有的回文串 再O(n*n)DP 输出路径dfs 刚开始存所有回文 ME了 后来发现用不着 改了改了OK了 数据还挺强 #include <iostream> #incl ...
- Light OJ 1033 - Generating Palindromes(区间DP)
题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串. ============================================================= ...
- Ural 1635 Mnemonics and Palindromes(DP)
题目地址:space=1&num=1635">Ural 1635 又是输出路径的DP...连着做了好多个了. . 状态转移还是挺简单的.要先预处理出来全部的回文串,tag[i] ...
- 【UVa】Partitioning by Palindromes(dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=sh ...
- 1033 - Generating Palindromes
1033 - Generating Palindromes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
随机推荐
- java附件上传下载磁盘版
ACTION public class UploadAction extends BaseAction { private static final long serialVersionUID = 1 ...
- 【STM32学习笔记1】基于固件库的STM32_MDK工程模版
文章包含STM32固件库介绍和工程模板搭建两方面内容. 一.STM32固件库介绍 要建立工程模板,首先要对STM32的固件库有所了解.STM32的固件可以从ST官网下载,网址为:http://www. ...
- javascript 修改css样式
abc.css CSS code .class1 { width:10px; background-color: red; } HTML code <!DOCTYPE ...
- BASE64URL解析
BASE64URL是一种在BASE64的基础上编码形成新的加密方式,为了编码能在网络中安全顺畅传输,需要对BASE64进行的编码,特别是互联网中. BASE64URL编码的流程: .明文使用BASE6 ...
- JQuery DataTable插件
参考文件: http://blog.csdn.net/xuechongyang/article/details/8424897 http://blog.csdn.net/llhwin2010/arti ...
- 获取当前WEB应用全路径
<%String path = request.getContextPath();String basePath =request.getScheme()+"://"+req ...
- jar 包和批量处理
打jar包: 选择要打包的文件: javac -d . Hello.java .代表打包的目标路径 jar cvf test.jar com //test是打包名称,com是打包的文件夹 修改tes ...
- IE 弹出框处理经验
//各屏幕弹出窗样式 // 1366*768var style_1366x768 = "dialogWidth:950px;dialogHeight:650px;help:no;center ...
- PHP中类的继承关系
在PHP中,我时常会写一个类,类写了一个共用方法,然后让子类去继承就能得到相应的功能.假设大致有这么一个父类: 1 <?php 2 class Father{ 3 4 public functi ...
- 高性能javascript 学习笔记(1)
加载和运行 管理浏览器中的javascript代码是个棘手的问题,因为代码运行阻塞了其他浏览器处理过程,诸如用户绘制,每次遇到<script>标签,页面必须停下来等待代码下载(如果是外部的 ...