求个LCS, 只是有了限制, 多加一维表示匹配到z串的第几个, 然后用滚动数组
----------------------------------------------------------------------------
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define x(i) x[i - 1]
#define y(i) y[i - 1]
#define z(i) z[i - 1]
const int maxn = 509;
char x[maxn], y[maxn], z[maxn];
int dp[2][maxn][maxn], xn, yn, zn;
int main() {
scanf("%s%s%s", x, y, z);
xn = strlen(x); yn = strlen(y); zn = strlen(z);
memset(dp, 0, sizeof dp);
int c = 0, p = 1;
for(int i = 1; i <= xn; i++) {
swap(c, p);
memset(dp[c], 0, sizeof dp[c]);
for(int j = 1; j <= yn; j++)
for(int k = 0; k <= zn; k++) {
dp[c][j][k] = max(max(dp[p][j][k], dp[c][j - 1][k]), dp[c][j][k]);
if(x(i) == y(j) && (k == 0 || dp[p][j - 1][k])) {
if(x(i) == z[k])
dp[c][j][k + 1] = max(dp[c][j][k + 1], dp[p][j - 1][k] + 1);
else
dp[c][j][k] = max(dp[c][j][k], dp[p][j - 1][k] + 1);
}
}
}
if(dp[c][yn][zn])
printf("%d
", dp[c][yn][zn]);
else
puts("NO SOLUTION");
return 0;
}
----------------------------------------------------------------------------
3304: [Shoi2005]带限制的最长公共子序列
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 307 Solved: 137
[Submit][Status][Discuss]
Description
Input
输入共三行,每行为长度不超过500的,小写字母组成的非空字符串
按顺序分别表示x,y,z
Output
如存在满足条件的N,输出W的长度,否则输出 NO SOLUTION
Sample Input
helloworld
hellxebore
xr
Sample Output
5
HINT
w=hxeor
本题要求找出的W首先是X与Y的公共子序列并且包含Z,然后才是满足这些条件的
字符串里面找最长的。