hdu 1560 DNA sequence(IDA*)

来源:互联网 发布:配置ubuntu镜像站 编辑:程序博客网 时间:2024/05/22 08:26

点击打开链接

IDA* :限制dfs深度+最优性cut

#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int N=10;int n;char DNA[5]={"ACGT"},s[N][N];int pos[N];//pos[i]:s[i]目前匹配的字符个数 int size[N];//char seq[N];//母串 int depth;//限制dfs深度(母串长度) bool flag;int H(){int res=0;for(int i=0;i<n;i++)res=max(res,size[i]-pos[i]); //差最多的那个串 if(!res)//{flag=true;}return res;}int dfs(int cur){if(cur>depth) return false;if(cur+H()>depth)//最优性cut:当前使用长度+至少还要多少长度>限定长度时退出   return false;for(int i=0;i<4;i++) {int tmp[N];memcpy(tmp,pos,sizeof(pos));//回溯时用 for(int j=0;j<n;j++)//检查匹配情况 {if(s[j][pos[j]]==DNA[i])pos[j]++;}if(dfs(cur+1)||flag)return true;memcpy(pos,tmp,sizeof(pos));//回溯 }}int main(){int t;cin>>t;while(t--){depth=0;flag=false;memset(pos,0,sizeof(pos));cin>>n;for(int i=0;i<n;i++){scanf("%s",s[i]);size[i]=strlen(s[i]);}while(!dfs(0)&&!flag){depth++;}cout<<depth<<endl;}return 0;}


0 0
原创粉丝点击