示例题目:HDU1159
参考资料:《挑战程序设计竞赛》
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38171 Accepted Submission(s): 17503
Problem Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm> another sequence Z = < z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c> is a subsequence of X = < a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
Source
Southeastern Europe 2003
题目大意:最长公共子序列
解题思路:
dp[i][j]:s[1…i]和t[1…j]对应的LCS的长度
状态转移方程:
dp[i+1][j+1]={dp[i][j]+1(si+1=tj+1)max(dp[i][j+1],dp[i+1][j])(其他)
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=600;
char s[MAXN],t[MAXN];
int dp[MAXN+1][MAXN+1];
int main()
{
ios::sync_with_stdio(false);
while(cin>>s>>t)
{
memset(dp,0,sizeof(dp));
int slen=strlen(s);
int tlen=strlen(t);
for(int i=0;i<slen;i++)
{
for(int j=0;j<tlen;j++)
{
if(s[i]==t[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
}
}
cout<<dp[slen][tlen]<<endl;
}
return 0;
}
滚动数组优化
示例题目: HDU1513
Palindrome
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5834 Accepted Submission(s): 1955
Problem Description
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.
Input
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.
Output
Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.
Sample Input
5
Ab3bd
Sample Output
2
Source
IOI 2000
题目大意: 给定一个字符串,把它变成回文串,求添加最少的字符数。
解题思路: 将原字符串翻转,求LCS,则剩下的字符就是要添加配对的,由于字符串长度可达5000,直接开二维会MLE,所以用滚动数组优化,填一个2行n列的表。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=5005;
char s[MAXN],t[MAXN];
int dp[2][MAXN+1];
char s1[MAXN],s2[MAXN];
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n)
{
memset(dp,0,sizeof(dp));
cin>>s1;
for(int i=0;i<n;i++)
s2[i]=s1[n-1-i];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(s1[i-1]==s2[j-1])
dp[i%2][j]=dp[(i-1)%2][j-1]+1;
else
dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);
}
}
cout<<n-dp[n%2][n]<<endl;
}
return 0;
}