HDU 1560 DNA sequence DFS

时间:2024-01-18 12:32:38

题意:找到一个最短的串,使得所有给出的串是它的子序列,输出最短的串的长度,然后发现这个串最长是40

分析:从所给串的最长长度开始枚举,然后对于每个长度,暴力深搜,枚举当前位是哪一个字母,注意剪枝

注:然后我看网上都说这叫迭代加深搜索

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
struct Node
{
int len;
char s[];
} o[];
char c[]="AGCT";
int pos[],T,n,deep;
int check()
{
int ans=;
for(int i=; i<=n; ++i)
ans=max(ans,o[i].len-pos[i]);
return ans;
}
bool dfs(int step)
{
if(step+check()>deep)return ;
if(!check())return ;
int tmp[];
for(int i=; i<=n; ++i)
tmp[i]=pos[i];
for(int i=; i<; ++i)
{
bool flag=;
for(int j=; j<=n; ++j)
{
if(o[j].s[pos[j]]==c[i])
{
pos[j]++;
flag=;
}
}
if(flag)
{
if(dfs(step+))return ;
for(int i=;i<=n;++i)
pos[i]=tmp[i];
}
}
return ;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int mx=;
for(int i=; i<=n; ++i)
{
scanf("%s",o[i].s);
o[i].len=strlen(o[i].s);
mx=max(mx,o[i].len);
}
memset(pos,,sizeof(pos));
deep=mx;
for(;;++deep)
if(dfs())break;
printf("%d\n",deep);
}
return ;
}