poj1159 Palindrome
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
Input
Output
Sample Input
5
Ab3bd
Sample Output
2
题解:回文串:要求得到一个字符串变为回文串要插入字符的最少数
输入a字符串的长度,再输入a字符串;
将a字符串复制到b中(相当于将a字符串反置)
求出其与a的最长公共子序列len;
则min=strlen(a)-len
Eg.
A: Ad3db
B: bd3dA
最长公共子序列为d3d
所以len=3;
Min=n-len=2
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
short d[][];
int dp(char *s, int n)
{
int i, j, k;
for (i=n-;i>=;i--)
{
for (j=i+;j<n;j++)
{
if (s[i] == s[j])
{
d[i][j] = d[i+][j-];
}
else
{
d[i][j] = min(d[i][j-], d[i+][j]) + ;
}
}
}
return d[][n-];
} int main( )
{
char str[];
int n;
while (scanf("%d", &n) ==)
{
scanf("%s",str);
printf("%d\n", dp(str, n));
}
return ;
}
poj1159 Palindrome的更多相关文章
- POJ1159——Palindrome(最长公共子序列+滚动数组)
Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...
- POJ1159 Palindrome(数位DP)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 58277 Accepted: 20221 Desc ...
- POJ1159——Palindrome
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53647 Accepted: 18522 Desc ...
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...
- [POJ1159]Palindrome(dp,滚动数组)
题目链接:http://poj.org/problem?id=1159 题意:求一个字符串加多少个字符,可以变成一个回文串.把这个字符串倒过来存一遍,求这两个字符串的lcs,用原长减去lcs就行.这题 ...
- POJ1159 - Palindrome(区间DP)
题目大意 给定一个字符串S,问最少插入多少个字符可以使字符串S变为回文串 题解 用dp[i][j]表示把字符串s[i-j]变为回文串需要插入的最小字符数 如果s[i]==s[j]那么dp[i][j]= ...
- POJ1159 Palindrome(dp)
题目链接. 分析: 感叹算法的力量. 方法一: 设 dp[i][j] 为字符串 s, 从 i 到 j 需要添加的最少字符数. 那么如果 s[i] == s[j], dp[i][j] = dp[i+1] ...
- POJ1159:Palindrome(LCS小应用 回文)
地址:http://poj.org/problem?id=1159 题目需求: 给你一个字符串,求最少添加多少字符可以使之构成回文串. 题目解析: 简单做法是直接对它和它的逆序串求最长公共子序列长度l ...
- poj分类解题报告索引
图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...
随机推荐
- div+css模式编写html静态网页例子_仿照网页制作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java中的数据类型及相互转换方法
本文主要讲解两个部分: 一.Java中的数据类型有哪些? 二.数字类型和字符串类型相互转换的方法? 一.Java中的数据类型有哪些: Java中的数据类型有:基本数据类型和引用数据类型: 基本数据类型 ...
- HashMap,TreeMap,LinkedHashMap学习
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...
- java 服务端解决ajax跨域问题
//过滤器方式 可以更改为拦截器方式public class SimpleCORSFilter implements Filter { public void doFilter(ServletRequ ...
- [Javascript] Logging Pretty-Printing Tabular Data to the Console
Learn how to use console.table to render arrays and objects in a tabular format for easy scanning ov ...
- padding与margin的差别
之前一直没有搞懂android:padding和android:layout_margin的差别,事实上概念非常easy,padding是站在父view的角度描写叙述问题,它规定它里面的内容必须与这个 ...
- 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之三
/** ****************************************************************************** * @author 暴走的小 ...
- 9.16noip模拟试题
题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧(好吧,我自重).早苗的 ...
- c# hasvalue属性
// 数据类型? 表示参数的值可以为null空,此时这个参数可调用属性hasvalue来判断,此参数是否有除了null以外的值;进而进行其它的工作 //必须要加?才可用hasvalue属性 priva ...
- C#解leetcode 18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...