Description
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.
Input
Output
Sample Input
Sample Output
IDA*,temp[]記錄每個字符串搜索到第幾位
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define N 10
struct node{
int len;
string s;
}a[N];
int n;
int pos[N],depth,ans;
char dna[]="ACGT";
int calen(int *s){
int m=0;
for (int i=0;i<n;i++)
m=max(m,a[i].len-s[i]);
return m;
}
bool dfs(int *s,int now){
if (now+calen(s)>depth)
return false;
if (calen(s)==0)
return true;
int temp[N];
for (int i=0;i<4;i++){
bool flag=false;
memcpy(temp,s,sizeof(temp));
for (int j=0;j<n;j++){
if (a[j].s[temp[j]]==dna[i]){
flag=true;
temp[j]++;
}
}
if (flag){
if (dfs(temp,now+1))
return true;
}
}
return false;
}
int main(){
int t;
scanf("%d",&t);
while (t--){
scanf("%d",&n);
for (int i=0;i<n;i++){
cin>>a[i].s;
a[i].len=a[i].s.length();
}
memset(pos,0,sizeof(pos));
depth=1;
while (1){
if (dfs(pos,0))
break;
depth++;
}
printf("%d\n",depth);
}
return 0;
}