POJ3080Blue Jeans

http://poj.org/problem?id=3080

题意 : 给你几个DNA序列,让你找他们的共同的最长的子串,若是子串长度小于3,就输出no significant commonalities,否则就输出公共的那个子串,若是多个子串长度相同,就输出字典序最小的那一个子串。

思路 : 这个题的话就暴搜加枚举。。。。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std ;
 5 int main()
 6 {
 7     char ch[110][110],sh[110],zh[110];
 8     int m ;
 9     scanf("%d",&m);
10     for(int i = 1 ; i <= m ; i++)
11     {
12         int n ;
13         scanf("%d",&n) ;
14         zh[0] = '';
15         for(int j = 1 ; j <= n ; j++)
16             scanf("%s",ch[j]);
17         int len = strlen(ch[1]) ;
18         //两个循环找子串,呃,子串很多.........
19         for(int j = 0 ; j < 60 ; j++)
20         {
21             for(int k = 60-j ; k >= 2 ; k--)
22             {
23                 strncpy(sh,ch[1]+j,k);//字符串复制函数,将ch[1]串复制k个字符给sh串
24                 sh[k] = '';//加结束标志符
25                 int h ;
26                 for(h = 2 ; h <= n ; h++)
27                     if(strstr(ch[h],sh) == NULL)//如果某一字符串中没有子串就直接结束掉此循环
28                         break ;
29                 if(h <= n)//如果上边不正常结束循环了,代表着这个不是公共子串,直接跳出循环即可
30                 continue  ;
31                 if(h > n)
32                 {
33                     if(strlen(sh) == strlen(zh)&&strcmp(zh,sh) > 0)
34                     //如果有多个公共子串,输出字典序最小的那个
35                         strcpy(zh,sh);
36                     if(strlen(sh) > strlen(zh))
37                         strcpy(zh,sh) ;
38                 }
39             }
40         }
41         if(strlen(zh) > 2)
42             puts(zh);
43         else printf("no significant commonalities
");
44     }
45     return 0;
46 }
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3268776.html