1033 - Generating Palindromes
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
|
6 abcd aaaa abc aab abababaabababa pqrsabcdpqrs |
Case 1: 3 Case 2: 0 Case 3: 2 Case 4: 1 Case 5: 0 Case 6: 9 |
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<string.h>
6 #include<queue>
7 #include<stack>
8 #include<math.h>
9 using namespace std;
10 int dp[200][200];
11 char str[200];
12 char astr[200];
13 int main(void)
14 {
15 int i,j,k;
16 int s;
17 scanf("%d",&k);
18 for(s=1; s<=k; s++)
19 {
20 scanf("%s",str);
21 int l=strlen(str);
22 int u=0;
23 for(i=l-1; i>=0; i--)
24 {
25 astr[u++]=str[i];
26 }
27 for(i=0; i<200; i++)
28 {
29 for(j=0; j<200; j++)
30 {
31 dp[i][j]=1e9;
32 }
33 }
34 int maxx=0;
35 for(i=0; i<l; i++)
36 {
37 for(j=i; j>=0; j--)
38 {
39 if(i==j)
40 {
41 dp[j][i]=0;
42 }
43 else
44 {
45 if(str[i]==str[j])
46 {
47 dp[j][i]=dp[j+1][i-1];
48 if(j+1>i-1)
49 {
50 dp[j][i]=0;
51 }
52 }
53 else
54 {
55 dp[j][i]=min(dp[j][i-1],dp[j+1][i])+1;
56 }
57 }
58 }
59 }
60 printf("Case %d: %d\n",s,dp[0][l-1]);
61 }
62 return 0;
63 }
1033 - Generating Palindromes的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- Light OJ 1033 - Generating Palindromes(区间DP)
题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串. ============================================================= ...
- Generating Palindromes LightOJ - 1033
Generating Palindromes LightOJ - 1033 题意:添加最少的字符使得给出的字符串成为回文串.输出添加的字符数. 方法:常规区间dp.ans[i][j]表示使得ans[i ...
- LightOJ1033 Generating Palindromes(区间DP/LCS)
题目要计算一个字符串最少添加几个字符使其成为回文串. 一年多前,我LCS这道经典DP例题看得还一知半解时遇到一样的问题,http://acm.fafu.edu.cn/problem.php?id=10 ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- dp百题大过关(第一场)
好吧,这名字真是让我想起了某段被某教科书支配的历史.....各种DP题层出不穷,不过终于做完了orz 虽然各种手糊加乱搞,但还是要总结一下. T1 Monkey Banana Problem 这 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- 【hihoCoder】1033: 交错和
初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...
- Codeforces 722D. Generating Sets
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- 从零开始学习oracle
引用博客:https://blog.csdn.net/qq_36998053/article/details/82725765 )Oracle之<环境配置> (二)Oracle之<基 ...
- Requests的安装和使用
一.Requests的安装1.pip3 install requests2.验证 import requests 不报错即可
- android studio Please configure Android SDK / please select Android SDK
有可能是sdk文件损坏造成的. file->settings->appearance&behavior->system settings->android sdk-&g ...
- Linux 内存泄漏 valgrind
Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgrind 安装: 去官网下载: http://valgrind.org/downloads/curre ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- Android WifiP2p实现
Android WifiP2p实现 Wifi Direct功能早在Android 4.0就以经加入Android系统了,但是一直没有很好的被支持,主要原因是比较耗电而且连接并不是很稳定.但是也有很大的 ...
- Docker常用image
MySQL Start a mysql server instance Starting a MySQL instance is simple: docker run -itd --name mysq ...
- Django REST framework完全入门
Django REST framework 一个强大灵活的Django工具包,提供了便捷的 REST API 开发框架 我们用传统的django也可以实现REST风格的api,但是顶不住Django ...
- numpy基础教程--将二维数组转换为一维数组
1.导入相应的包,本系列教程所有的np指的都是numpy这个包 1 # coding = utf-8 2 import numpy as np 3 import random 2.将二维数组转换为一维 ...
- JuiceFS 缓存策略详解
对于一个由对象存储和数据库组合驱动的文件系统,缓存是本地客户端与远端服务之间高效交互的重要纽带.读写的数据可以提前或者异步载入缓存,再由客户端在后台与远端服务交互执行异步上传或预取数据.相比直接与远端 ...