一.DP求回文串[POJ-1159]

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.

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.

输入描述

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from ‘A’ to ‘Z’, lowercase letters from ‘a’ to ‘z’ and digits from ‘0’ to ‘9’. Uppercase and lowercase letters are to be considered distinct.

输出描述

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

输入样例

5
Ab3bd

输出样例

2

题目描述:

给你一个字符串,问最少需要添加几个字符可以将该字符串变为回文串

设原序列S的逆序列为S’,最少需要补充的字母数 = 原序列S的长度 — S和S’的最长公共子串长度

代码如下:

//题目意思呢就是给你一串字符,问最少需要插入几个字符能形成回文字符串
//解题思路:这就是求该字符串正序和逆序最长公共子序列问题
//这样就可以找到对称着的最长公共子序列了,然后用总长度减去这个就是我们要求的了。
//这道题超内存, 所以要用
#include<iostream>
using namespace std;
const int N = 5010;
char s1[N], s2[N];
int d[2][N];
int main()
{int n;cin >>n;cin >> (s1 + 1);for(int i = 1; i <= n; i++)s2[i] = s1[n - i + 1];int ans = 0;int t = 0;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(s1[i] == s2[j])d[t][j] = d[t ^ 1][j - 1] + 1;else d[t][j] = max(d[t ^ 1][j], d[t][j - 1]);ans = max(ans, d[t][j]);}t ^= 1;}cout << n - ans << endl;return 0;
}

二.DP求变成回文串的最小花费[POJ-3280]

题目描述

Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag’s contents are currently a single string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).

Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is “abcba” would read the same no matter which direction the she walks, a cow with the ID “abcb” can potentially register as two different IDs (“abcb” and “bcba”).

FJ would like to change the cows’s ID tags so they read the same no matter which direction the cow walks by. For example, “abcb” can be changed by adding “a” at the end to form “abcba” so that the ID is palindromic (reads the same forwards and backwards). Some other ways to change the ID to be palindromic are include adding the three letters “bcb” to the begining to yield the ID “bcbabcb” or removing the letter “a” to yield the ID “bcb”. One can add or remove characters at any location in the string yielding a string longer or shorter than the original string.

Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ cost ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow’s ID tag and the cost of inserting or deleting each of the alphabet’s characters, find the minimum cost to change the ID tag so it satisfies FJ’s requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can be added to a string.

输入格式

Line 1: Two space-separated integers: N and M
Line 2: This line contains exactly M characters which constitute the initial ID string
Lines 3… N+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character.

输出格式

Line 1: A single line with a single integer that is the minimum cost to change the given name tag.

输入样例

3 4
abcb
a 1000 1100
b 350 700
c 200 800

输出样例

900

题目大意:

给你一个字符串,可以对每个字符串进行添加或删除,添加和删除都有对应的花费,问:变为回文串的最小花费

//我们用dp[i][j]表示将i~j位置的字符串变为回文串的最低消费。
//当str[i]==str[j]时:dp[i][j]==dp[i+1][j-1]
//当i+1到j是回文串时:dp[i][j]==dp[i+1][j] + min ( add[str[i]],reduce[str[i]] )
//当i到j-1是回文串时:dp[i][j]==dp[i][j-1] + min( add[str[j]],reduce[str[j]] )#include<iostream>
using namespace std;
const int N = 2010, INF = 0x3f3f3f3f;
char s[N];
int cost[N], d[N][N];
int main()
{int n, m;cin >>n >> m;cin >> (s + 1);char c;int a, b;for(int i = 1; i <= n; i++){cin >> c >> a >> b;cost[c - 'a'] = min(a, b);}//区间DPfor(int len = 2; len <= m; len++){ // 枚举长度for(int l = 1, r = len; r <= m; l++, r ++){  //枚举起点和终点d[l][r] = INF;if(s[l] == s[r])d[l][r] = d[l + 1][r - 1];else {d[l][r] = min(d[l][r], d[l + 1][r] + cost[s[l] - 'a']);d[l][r] = min(d[l][r], d[l][r - 1] + cost[s[r] - 'a']);}}}cout << d[1][m] << endl;return 0;
}

动态规划之回文串问题相关推荐

  1. 动态规划:回文串系列

    1.最长回文子串 寻找回文串的问题核心思想是:从中间开始向两边扩散来判断回文串 for 0 <= i < len(s):找到以 s[i] 为中心的回文串:(奇数)找到以 s[i] 和 s[ ...

  2. LeetCode 1278. 分割回文串 III

    截止到目前我已经写了 600多道算法题,其中部分已经整理成了pdf文档,目前总共有1000多页(并且还会不断的增加),大家可以免费下载 下载链接:https://pan.baidu.com/s/1hj ...

  3. 最长回文串--动态规划

    最长回文串–动态规划 参考:https://writings.sh/post/algorithm-longest-palindromic-substring class Solution {publi ...

  4. LeetCode之最大回文串--动态规划

    1. 题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1:输入: "babad"输出: "bab" 注意: ...

  5. 求最长回文串-从动态规划到马拉车之路(上)

    要解决的问题: 给定一个字符串,要求求出这个字符串中的最长的回文串子串. 例子: cbddba的最长回文子串为 bddb cbdedba的最长回文子串为dbedb 由上面的例子可以看到,在考虑回文子串 ...

  6. 【动态规划 回文串11】LeetCode 516. Longest Palindromic Subsequence

    LeetCode 516. Longest Palindromic Subsequence 本博客转载自:http://www.cnblogs.com/grandyang/p/6493182.html ...

  7. 【回文串5 重点+动态规划】LeetCode 132. Palindrome Partitioning II

    LeetCode 132. Palindrome Partitioning II Solution1:我的答案1 直接模仿131那道题的DFS解法,找其中size最小的.果不其然,因为超时只能部分AC ...

  8. 【回文串1 动态规划 马拉车算法】LeetCode 5. Longest Palindromic Substring

    LeetCode 5. Longest Palindromic Substring LeetCode中与回文串相关的免费的题目共有15道(5, 9, 125, 131, 132, 214, 234, ...

  9. 求最长回文串-从动态规划到马拉车之路(下)

    预备知识: (1)在一个数轴上有两点i和j(i<=j)关于点m对称,那么有 i = 2m-j: 证明: 因为 i<=j 且 i 和 j 关于 m 对称,那么有 (i + j)/ 2 = m ...

最新文章

  1. java 集合 自动排序的_java中的自动排序集合 ---- 20160809
  2. 编译安装openresty+mariadb+php7
  3. 第五期 IP数据包结构和OSI第三层网络层
  4. 给一个不多于5位的正整数,求出它是几位数?
  5. 灵活强大的构建系统Gradle
  6. oracle手动删除数据库
  7. 贝叶斯方法(Bayesian approach) —— 一种概率解释(probabilistic interpretation)
  8. byobu_如何使用byobu多路SSH会话
  9. 教大家抖音怎么开通直播功能
  10. Levenberg-Marquardt(LM算法)
  11. MATLAB---CAD绘制Bezier曲线算法
  12. Ubuntu 更改鼠标滚轮速度
  13. imwrite()和imshow()相关
  14. php 批量下载网页文件,批量下载文件(以xxx网站为例)
  15. RK3568 Android12 长按power键功能设置
  16. PTA 7-47 打印选课学生名单
  17. 公众号运营必备三大神器,满足你的所有需求
  18. 分期利息计算——考虑免息日期和多利率设定的思路设计和代码实现
  19. ELK日志管理系统的搭建
  20. jquery轮播插件

热门文章

  1. lintcode 中等题:Divide Two Integers 两个数的除法
  2. linux android开发环境搭建
  3. HttpHelper使用记录
  4. Hibernate的HQL中in参数设置和JdbcTemplete中的in参数
  5. mkdir 创建目录
  6. 给表空间增加数据文件
  7. cmd下运行java文件时,找不到或无法加载主类的解决方法
  8. 第三篇:白话tornado源码之请求来了
  9. C# 特性(Attribute)学习
  10. DevExpress AspxGridView数据绑定