HDU 1560 DNA sequence (迭代加深搜索)
The twenty-first century is a biology-technology developing century. We know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Finding the longest common subsequence between DNA/Protein sequences is one of the basic problems in modern computational molecular biology. But this problem is a little different. Given several DNA sequences, you are asked to make a shortest sequence from them so that each of the given sequence is the subsequence of it.
For example, given "ACGT","ATGC","CGTT" and "CAGT", you can make a sequence in the following way. It is the shortest but may be not the only one.
![](https://odzkskevi.qnssl.com/484ac84a1e7eb5b60c2d9f0ac6e356c7?v=1536322530)
For example, given "ACGT","ATGC","CGTT" and "CAGT", you can make a sequence in the following way. It is the shortest but may be not the only one.
InputThe first line is the test case number t. Then t test cases follow. In each case, the first line is an integer n ( 1<=n<=8 ) represents number of the DNA sequences. The following k lines contain the k sequences, one per line. Assuming that the length of any sequence is between 1 and 5.OutputFor each test case, print a line containing the length of the shortest sequence that can be made from these sequences.Sample Input
1 4 ACGT ATGC CGTT CAGT
Sample Output
8
思路
如果DNA最长的串长度为n,那就先搜索以n为长度,是否存在符合条件的母串,若不存在,再搜索n+1;
这便是所谓的迭代加深搜索。结束。
代码中用到的maxx,只是一个剪枝而已。
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #define fuck(x) cout<<#x<<" = "<<x<<endl; #define ls (t<<1) #define rs ((t<<1)+1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100086; const int inf = 2.1e9; const ll Inf = 999999999999999999; const int mod = 1e9+7; const double eps = 1e-6; char s[12][12]; int maxs=0; int tot[12]; int n,pos[12]; char a[5]="ACGT"; void view() { for(int i=1;i<=n;i++){ cout<<pos[i]<<" "; } cout<<endl; } bool dfs(int t) { int maxx=0; for(int i=1;i<=n;i++){ maxx=max(maxx,tot[i]-pos[i]); } if(maxx==0){return true;} if(t+maxx>maxs){return false;} bool vis[12]; for(int i=0;i<4;i++){ memset(vis,0,sizeof(vis)); for(int j=1;j<=n;j++){ if(s[j][pos[j]]==a[i]){ pos[j]++; vis[j]=true; } } if(dfs(t+1)){return true;} for(int j=1;j<=n;j++){ if(vis[j]){ pos[j]--; } } } return false; } int main() { int T; scanf("%d",&T); while(T--){ scanf("%d",&n); maxs=0; for(int i=1;i<=n;i++){ scanf("%s",s[i]); tot[i]=strlen(s[i]); maxs=max(maxs,tot[i]); } while(true){ memset(pos,0,sizeof(pos)); if(dfs(0)){ printf("%d\n",maxs); break; } else maxs++; } } return 0; }
如需转载,请注明出处
如有侵权,联系删除
2290713181@qq.com
如有侵权,联系删除
2290713181@qq.com
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现