5.DNA序列
原题:https://www.acwing.com/problem/content/submission/code_detail/24508041/
思路:IDA*根据每个字母开搜,也就是由“AGCT”开搜,记录每一个要枚举的字符串的枚举到的下标pos数组,启发函数使用最长的还未匹配的字符串的剩余字符数,注意恢复现场。
if(pos[j]&&s[j][pos[j]-1]==op[i]) pos[j]--;该语句恢复现场是错误的。
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N=110;
int n;
string s[N];
string op="AGCT";
int len[N],pos[N];
int f()
{
int res=0;
for(int i=0;i<n;i++)
{
res=max(res,len[i]-pos[i]);
}
return res;
}
bool dfs(int u,int depth)
{
if(f()+u>depth) return false;
if(f()==0) return true;
for(int i=0;i<4;i++)
{
bool flag=false;
int nums[N],cnt=0;
for(int j=0;j<n;j++)
{
if(s[j][pos[j]]==op[i])
{
pos[j]++;
nums[cnt++]=j;
flag=true;
}
}
if(flag)
{
if(dfs(u+1,depth)) return true;
for(int j=0;j<cnt;j++){
pos[nums[j]]--;
}
}
}
return false;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
int depth=0;
for(int i=0;i<n;i++)
{
cin>>s[i];
len[i]=s[i].size();
depth=max(depth,len[i]);
}
memset(pos,0,sizeof pos);
while(!dfs(0,depth)) depth++;
cout<<depth<<endl;
}
return 0;
}
浙公网安备 33010602011771号