Blue Jeans
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4119Accepted: 1707

Description

The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. 

As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers. 

A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC. 

Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
  • A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
  • m lines each containing a single base sequence consisting of 60 bases.

Output

For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

Sample Input

3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities
AGATAC
CATCATCAT

Source

首先把字符串连接起来,求sa,rank.height

二分答案,然后对height进行分组,在同一个分组中统计是否满足条件。

 

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 
  5 using namespace std;
  6 #define Max 610
  7 int mem[4][Max];
  8 int *rank;
  9 int *sa;
 10 int *nrank;
 11 int *nsa;
 12 int height[Max];
 13 int cnt[Max];
 14 int n;
 15 
 16 char s[Max];
 17 void make_sa()
 18 {
 19       memset(mem,0,sizeof(mem));
 20     sa = mem[0];
 21     rank = mem[1];
 22     nsa = mem[2];
 23     nrank = mem[3];
 24     memset(cnt, 0sizeof(cnt));
 25     for (int i = 0; i < n; ++i)
 26     {
 27         ++cnt[(int)s[i]];
 28     }
 29     for (int i = 1; i < 256++i)
 30     {
 31         cnt[i] += cnt[i-1];
 32     }
 33     for (int i = n-1; i >= 0--i)
 34     {
 35         sa[--cnt[(int)s[i]]] = i;
 36     }
 37     rank[sa[0]] = 0;
 38     for (int i = 1; i < n; ++i)
 39     {
 40         rank[sa[i]] = rank[sa[i-1]];
 41         if (s[sa[i]] != s[sa[i-1]])
 42         {
 43             ++rank[sa[i]];
 44         }
 45     }
 46     for (int k = 1; k < n && rank[sa[n-1]] < n-1; k *= 2)
 47     {
 48         for (int i = 0; i < n; ++i)
 49         {
 50             cnt[rank[sa[i]]] = i + 1;
 51         }
 52         for (int i = n-1; i >= 0--i)
 53         {
 54             if (sa[i]->= 0)
 55             {
 56                 nsa[--cnt[rank[sa[i]-k]]] = sa[i]-k;
 57             }
 58         }
 59         for (int i = n-k; i < n; ++i)
 60         {
 61             nsa[--cnt[rank[i]]] = i;
 62         }
 63         nrank[nsa[0]] = 0;
 64         for (int i = 1; i < n; ++i)
 65         {
 66             nrank[nsa[i]] = nrank[nsa[i-1]];
 67             if (rank[nsa[i]] != rank[nsa[i-1]] || rank[nsa[i]+k] != rank[nsa[i-1]+k])
 68             {
 69                 ++nrank[nsa[i]];
 70             }
 71         }
 72         swap(rank, nrank);
 73         swap(sa, nsa);
 74     }
 75 }
 76 
 77 void get_lcp_rmq()
 78 {
 79     int i, j, k;
 80     for (i = 0, k = 0; i < n; ++i)
 81     {
 82         if (rank[i]==0)
 83         {
 84             height[0= k = 0;
 85         }
 86         else
 87         {
 88             if (k > 0)
 89             {
 90                 --k;
 91             }
 92             j = sa[rank[i]-1];
 93             for (; s[i+k] == s[j+k]; ++k);
 94             height[rank[i]] = k;
 95         }
 96     }
 97 }
 98 int num;
 99 int stMark;
100 bool solve(int len)
101 {
102    // printf("ss\n");
103     for(int i=1;i<n;i++)
104     {
105         bool flag[10];
106         memset(flag,false,sizeof(flag));
107         int t;
108         while(i<n&&height[i]>=len)
109         {
110             flag[sa[i-1]/60= true;
111             flag[sa[i]/60= true;
112             t = sa[i-1];
113             i++;
114         }
115         int tcnt = 0;
116         for(int i=0;i<10;i++)
117         {
118             if(flag[i])
119             {
120                 tcnt++;
121             }
122         }
123         if(tcnt==num)
124         {
125             stMark = t;
126             return true;
127         }
128 
129     }
130     return false;
131 }
132 void  work()
133 {
134     int l = 3,r = 61;
135     bool mark = false;
136     int ans=0;
137     while(l<r)
138     {
139         int m = l+(r-l)/2;
140         if(solve(m)){ans=m;mark = true;l = m+1;}
141         else r = m;
142     }
143     if(!mark)
144     {
145         printf("no significant commonalities\n");
146     }
147     else
148     {
149         int st = stMark;
150         int cnt = 0;
151         while(cnt<ans)
152         {
153             printf("%c",s[st]);
154             cnt++;
155             st++;
156         }
157         printf("\n");
158     }
159 }
160 int main()
161 {
162     int T;
163     scanf("%d",&T);
164     while(T--)
165     {
166         memset(s,0,sizeof(s));
167         memset(height,0,sizeof(height));
168         scanf("%d",&num);
169         int st = 0;
170         for(int i=0;i<num;i++)
171         {
172             scanf("%s",s+st);
173             st+=60;
174             s[st++= i;
175         }
176         n = st;
177         make_sa();
178         get_lcp_rmq();
179         work();
180     }
181     return 0;
182 }
183