# BZOJ 3304: [Shoi2005]带限制的最长公共子序列( LCS )

----------------------------------------------------------------------------

#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\n", dp[c][yn][zn]);
else
puts("NO SOLUTION");
return 0;
}

----------------------------------------------------------------------------

## 3304: [Shoi2005]带限制的最长公共子序列

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 307  Solved: 137
[Submit][Status][Discuss]

helloworld
hellxebore
xr

5

w=hxeor

## Source

posted @ 2015-10-23 14:25  JSZX11556  阅读(340)  评论(0编辑  收藏