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.
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 ",maxs); break; } else maxs++; } } return 0; }