标签:
用N个串中找到最短的公共串(不要求连续,只要相对位置一样即可)
迭代加深搜索即可
剪枝:当前的深度+最少还有加深的深度是否大于限制的长度,若是,则退回。
#include "stdio.h"
#include "string.h"
const char ch[10]="ATCG";
int deep,n;
char s[10][10];
int pos[10];
int Max(int a,int b)
{
if (a<b) return b;else return a;
}
int get_h()
{
int ans,i;
ans=0;
for (i=0;i<n;i++)
ans=Max(ans,strlen(s[i])-pos[i]);
return ans;
}
int dfs(int w)
{
int h,i,j,flag;
int temp[10];
h=get_h();
if(w+h>deep) return 0;
if(h==0) return 1;
for (i=0;i<4;i++)
{
flag=0;
for (j=0;j<n;j++)
temp[j]=pos[j];
for (j=0;j<n;j++)
if (s[j][pos[j]]==ch[i])
{
flag=1;
pos[j]++;
}
if (flag==1)
{
if (dfs(w+1)==1) return 1;
for (j=0;j<n;j++)
pos[j]=temp[j];
}
}
return 0;
}
int main()
{
int Case,i;
scanf("%d",&Case);
while (Case--)
{
scanf("%d",&n);
deep=0;
for (i=0;i<n;i++)
{
scanf("%s",s[i]);
deep=Max(deep,strlen(s[i]));
pos[i]=0;
}
while (1)
{
if (dfs(0)==1) break;
deep++;
}
printf("%d\n",deep);
}
return 0;
}标签:
原文地址:http://blog.csdn.net/u011932355/article/details/44243253