周赛A题
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题的更多相关文章
- CSDN 轻松周赛赛题:能否被8整除
轻松周赛赛题:能否被8整除 题目详情 给定一个非负整数,问能否重排它的全部数字,使得重排后的数能被8整除. 输入格式: 多组数据,每组数据是一个非负整数.非负整数的位数不超过10000位. 输出格式 ...
- codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]
就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...
- 周赛F题 POJ 1458(最长公共子序列)
F - F Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description ...
- 周赛D题
D - D Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description ...
- leetcode 第184场周赛第一题(数组中的字符串匹配)
一.函数的运用 1,strstr(a,b); 判断b是否为a的子串,如果是,返回从b的开头开始到a的结尾 如“abcdefgh” “de” 返回“defgh”: 如果不是子串,返回NULL: 2,me ...
- Java实现 LeetCode第30场双周赛 (题号5177,5445,5446,5447)
这套题不算难,但是因为是昨天晚上太晚了,好久没有大晚上写过代码了,有点不适应,今天上午一看还是挺简单的 5177. 转变日期格式 给你一个字符串 date ,它的格式为 Day Month Yea ...
- 周赛C题 LightOJ 1047 (DP)
C - C Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Description Th ...
- LeetCode | 力扣周赛C题 5370. 设计地铁系统
请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: checkIn(int id, string stationName, int t) 编号为 id 的乘客在 t 时刻进 ...
- LeetCode周赛#208
本周周赛的题面风格与以往不太一样,但不要被吓着,读懂题意跟着模拟,其实会发现并不会难到哪里去. 1599. 经营摩天轮的最大利润 #模拟 题目链接 题意 摩天轮\(4\)个座舱,每个座舱最多可容纳\( ...
随机推荐
- python多线程机制
Python中的线程从一开始就是操作系统的原生线程.而Python虚拟机也同样使用一个全局解释器锁(Global Interpreter Lock,GIL)来互斥线程多Python虚拟机的使用. GI ...
- Jenkins 八: 构建Git项目
1. 安装git. http://git-scm.com/download/win 下载之后一步步安装即可. 2. 安装插件. 打开"系统管理" –> "管理插 ...
- @property中有哪些属性关键字?/ @property 后面可以有哪些修饰符?
出题者简介: 孙源(sunnyxx),目前就职于百度 整理者简介:陈奕龙(子循),目前就职于滴滴出行. 转载者:豆电雨(starain)微信:doudianyu 属性可以拥有的特质分为四类: 原子性- ...
- Java中的 修饰符
java中的修饰符分为类修饰符,字段修饰符,方法修饰符. 根据功能的不同,主要分为以下几种. 1.权限访问修饰符 访问权限的控制常被称为具体实现的隐藏 把数据和方法包进类中,以及具体实现的隐藏,常共 ...
- javaIO流小结(1)
UTF-8的字节占多少个字节? 常用中文字符用utf-8编码占用3个字节(大约2万多字),超大字符集中要占4个字节.在内存中是2个字节,真正写到硬盘上面的是3个字节. GBK.GB2312汉字占2个字 ...
- web.xml基本配置描述
先加载一段写好的web.xml: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2. ...
- storyBoard方式ScrollView的AutoLayout
在使用storyboard和xib时,我们经常要用到ScrollView,还有自动 布局AutoLayout,但是ScrollView和AutoLayout 结合使用,相对来说有点复杂.根据实践,我说 ...
- Dump 文件生成与分析
近期两天因为项目的须要,研究了一下Dump文件相关的知识,今天做一个小节(因为研究不久而且第一次写blog,希望网友们看到不要见笑). Dump文件是进程的内存镜像.能够把程序的运行状态通过调试器保存 ...
- Hibernate知识点总结
Hibernate配置二级缓存: --- 使用EhCache 1.hibernate.cfg.xml中配置二级缓存 <hibernate-configuration> <ses ...
- 全球最低功耗蓝牙单芯片(DA14580)系统架构和应用开发框架分析
DA14580是Dialog公司研制的蓝牙单芯片,号称全球功耗最低,是TI CC2541的四分之一,是运动手环等穿戴类电子产品的常用芯片.但是DA14580的开发门槛不低,适合有蓝牙开发经验的团队来开 ...