lcs变种
一开始我的想法是将题目给出的Favorite Color Stripe扩充后与实际的stripe做lcs,后来看到网上有更好的方法
m = max(mat[i - 1][j - 1], mat[i - 1][j], mat[i][j - 1])
当a[i] == b[i]时,mat[i][j] = m + 1;
反之,mat[i][j] = m;
from
http://blog.csdn.net/tiantangrenjian/article/details/19921051
#include<stdio.h>
#include<map>
#include<vector>
using namespace std;
int a[10001], color[201], b[10001];
int mat[10001][10001];
int max(const int &a, const int &b, const int &c){
if (a >= b&&a >= c)
return a;
if (b >= a&&b >= c)
return b;
if (c >= a&&c >= b)
return c;
}
int lcs(int a[], int alen, int b[], int blen){
int i, j;
for (i = 1; i <= alen; i++)
for (j = 1; j <= blen; j++)
{
int m = max(mat[i - 1][j - 1], mat[i - 1][j], mat[i][j - 1]);
if (a[i] == b[j])
mat[i][j] = m+1;
else mat[i][j] = m;
}
return mat[alen][blen];
}
int main(){
int m, n;
freopen("1.in", "r", stdin);
scanf("%d%d", &n, &m);
int i, col;
for (i = 1; i <= m; i++){
scanf("%d", &color[i]);
}
int len;
scanf("%d", &len);
for (i = 1; i <= len; i++)
scanf("%d", &a[i]);
printf("%d\n", lcs(a, len,color, m));
return 0;
}