一.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);
}
//区间DP
for(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;
}