【HDU 3294】Life Forms

Description

You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.

The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant‘s life forms ended up with a large fragment of common DNA.

Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.

Input

Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.

Output

For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output "?". Leave an empty line between test cases.

Sample Input

3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0

Sample Output

bcdefg
cdefgh

?

【题目大意】

给定多个字符串,寻找字符串中出现的最长相同子串,如果有多个全部输出

【解题思路】

首先求后缀数组,然后二分查找最长长度的子串

然后在二分答案的时候,记录答案

【代码细节】

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<vector>
  6 #include<cstring>
  7 using namespace std;
  8 const int MAXN = 110;
  9 const int MAXM = 2010;
 10 int n = 0, num;
 11 int down;
 12 int s[MAXN*MAXM];
 13 vector<int> ret;
 14 char tmp[MAXM];
 15 int  SA[MAXN*MAXM], height[MAXN*MAXM];
 16 int  rak[MAXN*MAXM],k;
 17 int  c[MAXN*MAXM], x[MAXN*MAXM], y[MAXN*MAXM];
 18 int  pos[MAXN],id[MAXN*MAXM];
 19 vector<int> ans;
 20 bool vis[MAXN];
 21 void init()
 22 {
 23     memset(c, 0, sizeof(c));
 24     memset(x, 0, sizeof(x));
 25     memset(y, 0, sizeof(y));
 26     return;
 27 }
 28 void get_SA(int *s)
 29 {
 30     int m = down;
 31     for (int i = 1; i <= n; i++)
 32         ++c[x[i] = s[i]];
 33     for (int i = 2; i <= m; i++)
 34         c[i] += c[i - 1];
 35     for (int i = n; i >= 1; i--)
 36         SA[c[x[i]]--] = i;
 37     for (int k = 1; k <= n; k <<= 1)
 38     {
 39         int num = 0;
 40         for (int i = n - k + 1; i <= n; i++)    y[++num] = i;
 41         for (int i = 1; i <= n; i++)
 42             if (SA[i] > k)
 43                 y[++num] = SA[i] - k;
 44         for (int i = 1; i <= m; i++)
 45             c[i] = 0;
 46         for (int i = 1; i <= n; i++)
 47             c[x[i]]++;
 48         for (int i = 2; i <= m; i++)
 49             c[i] += c[i - 1];
 50         for (int i = n; i >= 1; i--)
 51             SA[c[x[y[i]]]--] = y[i], y[i] = 0;
 52         swap(x, y);
 53         x[SA[1]] = 1; num = 1;
 54         for (int i = 2; i <= n; i++)
 55             x[SA[i]] = (y[SA[i]] == y[SA[i - 1]] && y[SA[i] + k] == y[SA[i - 1] + k]) ? num : ++num;
 56         if (num == n) break;
 57         m = num;
 58     }
 59     for (int i = 1; i <= n; i++)
 60         rak[SA[i]] = i;
 61     return;
 62 }
 63 void get_height(int *s)
 64 {
 65     int j, k = 0;
 66     for (int i = 1; i <= n; i++) {
 67         if (k) k--;
 68         j = SA[rak[i] - 1];
 69         while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++;
 70         height[rak[i]] = k;
 71     }
 72 }//以上为板子
 73 int sta[MAXN];
 74 int top = 0;//以下为核心
 75 bool check(int x)
 76 {
 77     int cnt = 0;
 78     bool flag = 0;
 79     memset(vis, 0, sizeof(vis));
 80     for (int i = 2; i < n; i++)
 81     {
 82         if (height[i] >= x)
 83         {
 84             if (!vis[id[SA[i]]]) cnt++, vis[id[SA[i]]] = 1;
 85             if (!vis[id[SA[i - 1]]]) cnt++, vis[id[SA[i - 1]]] = 1; 85.5       //查找到符合条件的子串
 86         }
 87         else
 88         {
 89             if (cnt > k)
 90             {
 91                 if (!flag) 91.5               //如果已经有解,清空 ans
 92                     ans.clear();
 93                 ans.push_back(SA[i-1]);
 94                 flag = true;
 95             }
 96             memset(vis, 0, sizeof(vis));
 97             cnt = 0;
 98         }
 99     }
102     return flag;
103 }
104 int main()
105 {
106     while(scanf("%d", &num))
107     {
108         if (num == 0)
109             break;
110         down =100;
111         init();
112         ret.clear();
113         n = 0;
114         for (int i = 1; i <= num; i++)
115         {
116             scanf("%s", tmp + 1);
117             int N = strlen(tmp+1);
118             for (int j = 1; j <= N; j++)
119             {
120                 s[++n] = tmp[j] - ‘a‘ + 1;
121                 id[n] = i;
122             }
123             s[++n] = ++down;
124             id[n] = -1;
125         }
126         get_SA(s);
127         get_height(s);
128         int l = 1, r = n;
129         int len = 0;
130         k = num / 2;
131         while (l <= r)
132         {
133
134             int mid = (l + r) >> 1;
135             if (check(mid))
136             {
137                 len = max(len, mid);
138                 l = mid + 1;
139             }
140             else
141             {
142                 r = mid - 1;
143             }
144         }
145
146         if    (len==1 || len==0)
147         {
148             printf("?\n\n");
149             continue;
150         }
151         for (int i = 0; i < ans.size(); i++)
152         {
153             for (int j = ans[i]; j <= ans[i] + len-1; j++)
154                 printf("%c", s[j]+‘a‘-1);
155             printf("\n");
156         }
157         printf("\n");
158     }
159
160     return 0;
161 }

原文地址:https://www.cnblogs.com/rentu/p/11515624.html

时间: 2024-10-13 21:32:08

【HDU 3294】Life Forms的相关文章

【POJ 3294】Life Forms 不小于k个字符串中的最长子串

一下午和一晚上都在刚这道题,各种错误都集齐了so sad 我的时间啊!!! 后缀数组就先做到这里吧,是在伤不起啊QAQ 出现了各种奇怪的错误,看了标算,然后乱改自己的代码,莫名其妙的改A了,后来发现用字符直接给int赋值会WA,必须一个字符先给另一个字符赋值,后者再给int赋值就能A(什么鬼).后来加了一个(int)s[n]强制转换就简单地A了,评测时强制转换睡觉了吗?还是我rp太差,得多攒点rp #include<cstdio> #include<cstring> #includ

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

【HDU 1009】FatMouse&#39; Trade

题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of c

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

不容易啊,终于可以补第二个题了!! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵,每一个格子里都将有一个数.给你每一行数字之和和每一列数字之和.求每一个位置能填0~k之间的哪个数.如果有多种可能输出"Not Unique",如果没有解输出"Impossible",如果一组解则将其输出. 解题思路: 最大流: 不可能的条件:是行之和和列之和不想等或者建图后的最大流与他们不想等. 多组的条件是:在最大流

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

[HDU 1839] Delay Constrained Maximum Capacity Path(二分+最短路) Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 481 Problem

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 5811】Colosseo(拓扑+输入优化)

[HDU 5811]Colosseo(拓扑+输入优化) Colosseo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 98 Problem Description Mr. Chopsticks keeps N monsters, numbered from 1 to N.