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 ...
随机推荐
- APM代码学习笔记3:执行过程
以Linux平台ArduPlane为例 \ArduPlane\Plane.cpp 定义Plane类 继承自AP_HAL::HAL::Callbacks ,获取hal对象. \ArduPlane\Ard ...
- IOS 物理引擎
来自IOS7 by tutorials 下面是个人的一点翻译总结 1,首先在初始化方法李画一个方块 UIView* square = [[UIView alloc] initWithFrame: ...
- Mac 键盘快捷键
标签页和窗口快捷键 ⌘-N 打开新窗口. ⌘-T 打开新标签页. ⌘-Shift-N 在隐身模式下打开新窗口. 按 ⌘-O,然后选择文件. 在 Google Chrome 浏览器中打开计算机中的文件. ...
- Win7下超级管理员创建普通权限任务
已转至新的博客 http://www.raysoftware点击打开链接.cn/?p=49 项目中用到一个功能,Win7下超级管理员创建普通权限任务. 试了几种办法,例如获取资源管理器的Token,然 ...
- javascript简单对象创建
由于javascript中定义了一个函数就相当于定义了一个类,我们当然可以创建一个这个类的对象. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...
- 【基础】常用的机器学习&数据挖掘知识点
Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...
- windows下搭建apache+php+mysql
在windows下,apache和mysql都有自动化安装的程序,本篇则侧重从apache和php版本选择,php线程安全,apache和mysql安装启动服务,工作环境配置这几个方面来阐述windo ...
- cuda核函数再调用核函数,多层并行
#include <stdio.h> __global__ void childKernel(int i) { int tid = blockIdx.x*blockDim.x+thread ...
- Objective-C分类 (category)和扩展(Extension)
1.分类(category) 使用Object-C中的分类,是一种编译时的手段,允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以 ...
- tomcat 7 无法打开管理页面
在配置文件tomcat-users.xml中添加如下内容即可. <role rolename="admin"/> <role rolename="man ...