SPOJPHRASES-Relevant Phrases of Annihilation

来源:互联网 发布:vb编写程序九九乘法表 编辑:程序博客网 时间:2024/06/05 09:04

PHRASES - Relevant Phrases of Annihilation

no tags 

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

Input

The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

Output

For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

Example

Input:14abbabbadabddkabababacabababaOutput:2

(in the example above, the longest substring which fulfills the requirements is 'ba')


题意:求每个字符串至少出现两次的且不重叠的最长公共子串

解题思路:将每个串用没出现过的字符进行连接,求出后缀数组后,二分答案+验证


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int N = 200010;int k;struct Sa{char ch[N], s[N];int vis[15], ma[15], mi[15];int rk[2][N], sa[N], h[N], w[N], now, n, len[N];int rmq[N][20], lg[N], bel[N];void GetS(){n = 0;scanf("%d", &k);for (int i = 1; i <= k; i++){scanf("%s", ch);for (int j = 0; ch[j]; j++) s[++n] = ch[j], bel[n] = i;s[++n] = i;len[i] = n;}}void getsa(int z, int &m){int x = now, y = now ^= 1;for (int i = 1; i <= z; i++) rk[y][i] = n - i + 1;for (int i = 1, j = z; i <= n; i++)if (sa[i] > z) rk[y][++j] = sa[i] - z;for (int i = 1; i <= m; i++) w[i] = 0;for (int i = 1; i <= n; i++) w[rk[x][rk[y][i]]]++;for (int i = 1; i <= m; i++) w[i] += w[i - 1];for (int i = n; i >= 1; i--) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];for (int i = m = 1; i <= n; i++){int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;}}void getsa(int m){now = rk[1][0] = sa[0] = s[0] = 0;for (int i = 1; i <= m; i++) w[i] = 0;for (int i = 1; i <= n; i++) w[s[i]]++;for (int i = 1; i <= m; i++) rk[1][i] = rk[1][i - 1] + (bool)w[i];for (int i = 1; i <= m; i++) w[i] += w[i - 1];for (int i = 1; i <= n; i++) rk[0][i] = rk[1][s[i]];for (int i = 1; i <= n; i++) sa[w[s[i]]--] = i;rk[1][n + 1] = rk[0][n + 1] = 0;for (int x = 1, y = rk[1][m]; x <= n&&y <= n; x <<= 1) getsa(x, y);for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j){if (rk[now][i] == 1) continue;int k = n - max(sa[rk[now][i] - 1], i);while (j <= k&&s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;}}void getrmq(){h[n + 1] = h[1] = lg[1] = 0;for (int i = 2; i <= n; i++)rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;for (int i = 1; (1 << i) <= n; i++){for (int j = 2; j <= n; j++){if (j + (1 << i) > n + 1) break;rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);}}}int lcp(int x, int y){int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);}bool check(int x){memset(vis, 0, sizeof vis);memset(mi, INF, sizeof mi);memset(ma, 0, sizeof ma);for (int i = 1; i <= n; i++){if (h[i] >= x){vis[bel[sa[i]]]++, vis[bel[sa[i - 1]]]++;ma[bel[sa[i]]] = max(ma[bel[sa[i]]], sa[i]);mi[bel[sa[i]]] = min(mi[bel[sa[i]]], sa[i]);ma[bel[sa[i - 1]]] = max(ma[bel[sa[i - 1]]], sa[i - 1]);mi[bel[sa[i - 1]]] = min(mi[bel[sa[i - 1]]], sa[i - 1]);}else{int flag = 1;for (int i = 1; i <= k; i++)if (vis[i] < 2 || ma[i] - mi[i] < x) { flag = 0; break; }if (flag) return 1;memset(vis, 0, sizeof vis);memset(mi, INF, sizeof mi);memset(ma, 0, sizeof ma);}}int flag = 1;for (int i = 1; i <= k; i++)if (vis[i] < 2 || ma[i] - ma[i] < x) { flag = 0; break; }return flag;}void work(){getsa(300);int l = 0, r = n, ans = 0;while (l <= r){int mid = (l + r) >> 1;if (check(mid)) l = mid + 1, ans = mid;else r = mid - 1;}printf("%d\n", ans);}}sa;int main(){int t;scanf("%d", &t);while (t--){sa.GetS();sa.work();}return 0;}

原创粉丝点击