#include <cstdio>
#include <cstring>
const int maxn = 10;
const char DNA[] = {"ACGT"};
char str[maxn][maxn];
int T, n, m, len[maxn], res[maxn];
bool dfs(int* cur, int x, int dep)
{
int tmp[maxn];
for (int i = 0; i < n; i++)
if (len[i] - cur[i] + x > dep) return 0;
bool ok = 1;
for (int i = 0; i < n; i++)
if (str[i][cur[i]]) ok = 0;
if (ok) return 1;
for (int i = 0; i < 4; i++)
{
ok = 0;
for (int j = 0; j < n; j++)
if (DNA[i] == str[j][tmp[j] = cur[j]])
{tmp[j]++; ok = 1;}
if (ok && dfs(tmp, x + 1, dep)) return 1;
}
return 0;
}
int main(int argc, char const *argv[])
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%s", str[i]);
len[i] = strlen(str[i]);
}
memset(res, 0, sizeof(res));
int ans = 0;
while (!dfs(res, 0, ans)) ans++;
printf("%d\n", ans);
}
return 0;
}
搜索题,容易超时。
串长不超过5,最多8个串,所以最短公共母串最长为40,所以IDA*搜索不超过40次