我发现我一直理解错题意了,这里的子序列指的是连续子序列,怪不得我写的LCS一直WA
顺便复习一下并查集
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
int dp[maxn][maxn], parent[maxn], len[maxn];
char DNA[maxn][maxn]; int LCS(int a, int b)
{
int ans = ;
memset(dp,,sizeof(dp));
for(int i = ; i <= len[a]; ++i)
for(int j = ; j <= len[b]; ++j)
{
if(DNA[a][i-] == DNA[b][j-])
dp[i][j] = dp[i-][j-] + ;
if(dp[i][j] > ans)
ans = dp[i][j];
}
return ans;
} int GetParent(int a)
{
return parent[a] == a ? a : parent[a] = GetParent(parent[a]);
} int main(void)
{
#ifdef LOCAL
freopen("2144in.txt", "r", stdin);
#endif int n, kase = ;
double p;
while(scanf("%d %lf", &n, &p) == )
{
for(int i = ; i < n; ++i)
{
scanf("%s", DNA[i]);
parent[i] = i;
len[i] = strlen(DNA[i]);
}
for(int i = ; i < n; ++i)
for(int j = ; j < i; ++j)
{
int pi = GetParent(i);
int pj = GetParent(j);
if(pi == pj) continue;
double x = LCS(i, j) * 100.0;
if(x/len[i]>p && x/len[j]>p)
parent[pi] = pj;
}
int ans = ;
for(int i = ; i < n; ++i)
if(parent[i] == i)
++ans;
printf("Case %d:\n%d\n", ++kase, ans);
}
return ;
}
代码君