您的位置:首页 > 产品设计 > UI/UE

HDU1560 DNA sequence 迭代深搜IDA*

2017-01-14 19:08 381 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560

题意是给你一些DNA串,要你输出一串字符串使得它能包含所有的DNA串的序列,要求其字典序最小,长度最短。

代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define scanfprint() freopen("input.txt","r",stdin)
#define printfprint() freopen("output.txt","w",stdout)
#define mem(a,b) memset(a,b,sizeof(a))
const int spot=1000+10;
const int edge=100000+10;
const int maxn=10;
const double pi=acos(-1.0);
const int mod=1e9+7;
const double ips=0.000001;
int n,m,a[maxn],len[maxn],have[maxn],maxlen=0;
char s[maxn][maxn],ss[maxn]="ACGT";
bool flag=0;
int f()
{
int ans=0,i;
for(i=1; i<=n; i++)
ans=max(ans,len[i]-have[i]);
return ans;
}
void dfs(int maxlen)
{
int i,j;
int h=f();
if(!h)
{
flag=1;
return;
}
if(h>maxlen)
return ;
bool ok=0;
int temp[maxn];
memcpy(temp,have,sizeof(temp));
for(i=1; i<=4; i++)
{
for(j=1; j<=n; j++)
if(s[j][have[j]]==ss[i-1])
ok=1,have[j]++;
if(ok)
{
dfs(maxlen-1);
if(flag)
return ;
}
memcpy(have,temp,sizeof(have));
}
}
int main()
{
int nn,i,j;
scanf("%d",&nn);
while(nn--)
{
maxlen=0;
flag=0;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%s",s[i]);
len[i]=strlen(s[i]);
have[i]=0;
maxlen=max(maxlen,len[i]);
}
for(;;)
{
dfs(maxlen);
if(flag)
break;
else
maxlen++;
}
printf("%d\n",maxlen);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: