首页 > 代码库 > SPOJ 220. Relevant Phrases of Annihilation(后缀数组多次不重叠子串)
SPOJ 220. Relevant Phrases of Annihilation(后缀数组多次不重叠子串)
题目大意:给定N个串,求每个串至少出现两次的最长子串。
解题思路:每个字符串至少出现两次且不可重叠的最长子串:二分枚举长度后在同一分组中对每一个字符串保留一个最小的位置和一个最大的位置,最后查看是否每个串在同一组中都有至少两个后缀,并且后缀的坐标差大于枚举的长度。
POJ Problem Set (classical)
220. Relevant Phrases of Annihilation
Problem code: PHRASES
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: 1 4 abbabba dabddkababa bacaba baba Output: 2
(in the example above, the longest substring which fulfills the requirements is ‘ba‘)
Added by: | Adrian Kosowski |
Date: | 2004-10-11 |
Time limit: | 9s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel Pentium G860 3GHz) |
Languages: | All except: NODEJS PERL 6 SCM chicken VB.net |
Resource: | DASM Programming League 2004 (problemset 1) |
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <ctime> #include <map> #include <set> #define eps 1e-9 ///#define M 1000100 ///#define LL __int64 #define LL long long ///#define INF 0x7ffffff #define INF 0x3f3f3f3f #define PI 3.1415926535898 #define zero(x) ((fabs(x)<eps)?0:x) #define mod 1000000007 #define Read() freopen("autocomplete.in","r",stdin) #define Write() freopen("autocomplete.out","w",stdout) #define Cin() ios::sync_with_stdio(false) using namespace std; inline int read() { char ch; bool flag = false; int a = 0; while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-'))); if(ch != '-') { a *= 10; a += ch - '0'; } else { flag = true; } while(((ch = getchar()) >= '0') && (ch <= '9')) { a *= 10; a += ch - '0'; } if(flag) { a = -a; } return a; } void write(int a) { if(a < 0) { putchar('-'); a = -a; } if(a >= 10) { write(a / 10); } putchar(a % 10 + '0'); } const int maxn = 200010; int wa[maxn], wb[maxn], wv[maxn], ws1[maxn]; int sa[maxn]; int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a+l] == r[b+l]; } void da(int *r, int *sa, int n, int m) { int i, j, p, *x = wa, *y = wb; for(i = 0; i < m; i++) ws1[i] = 0; for(i = 0; i < n; i++) ws1[x[i] = r[i]]++; for(i = 1; i < m; i++) ws1[i] += ws1[i-1]; for(i = n-1; i >= 0; i--) sa[--ws1[x[i]]] = i; for(j = 1, p = 1; p < n; j <<= 1, m = p) { for(p = 0, i = n-j; i < n; i++) y[p++] = i; for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j; for(i = 0; i < n; i++) wv[i] = x[y[i]]; for(i = 0; i < m; i++) ws1[i] = 0; for(i = 0; i < n; i++) ws1[wv[i]]++; for(i = 1; i < m; i++) ws1[i] += ws1[i-1]; for(i = n-1; i >= 0; i--) sa[--ws1[wv[i]]] = y[i]; for(swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++; } return ; } int rank[maxn], height[maxn]; void calheight(int *r, int *sa, int n) { int i, j, k = 0; for(i = 1; i <= n; i++) rank[sa[i]] = i; for(i = 0; i < n; height[rank[i++]] = k) for(k?k--:0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++); return; } char str1[maxn], str2[maxn]; int seq[maxn]; int hash[maxn]; char str[110][1010]; int vis[110]; int Find(int x) { int s = hash[x]; for(int i = 0; i < s; i++) { int len = strlen(str[i]); x -= len; } x -= s; return x; } int pos[20][2]; bool judge(int mid, int n, int m) { for(int i = 2; i <= n; i++) { memset(vis, 0, sizeof(vis)); vis[hash[sa[i-1]]]++; pos[hash[sa[i-1]]][0] = pos[hash[sa[i-1]]][1] = sa[i-1]; while(mid <= height[i]) { int xp = hash[sa[i]]; if(!vis[xp]) { vis[xp]++; pos[xp][1] = pos[xp][0] = sa[i]; i++; continue; } vis[xp]++; pos[xp][0] = max(pos[xp][0], sa[i]); pos[xp][1] = min(pos[xp][1], sa[i]); i++; } int k; for(k = 0; k < m; k++) { if(vis[k] < 2 || pos[k][0]-pos[k][1] < mid) break; } if(k == m) return true; } return false; } void Del(int n, int len, int m) { int l = 1; int r = len/2; int xans = 0; while(l <= r) { int mid = (l+r)>>1; if(judge(mid, n, m)) { l = mid+1; xans = mid; } else r = mid-1; } cout<<xans<<endl; } int main() { int T; cin >>T; int n; while(T--) { scanf("%d", &n); memset(hash, -1, sizeof(hash)); int ans = 0; int Min = maxn; for(int i = 0; i < n; i++) { scanf("%s",str[i]); int len = strlen(str[i]); Min = min(Min, len); for(int j = 0; j < len; j++) { seq[ans] = str[i][j]; hash[ans++] = i; } seq[ans++] = 200+i; } seq[ans] = 0; da(seq, sa, ans+1, 310); calheight(seq, sa, ans); Del(ans, Min, n); } return 0; }
SPOJ 220. Relevant Phrases of Annihilation(后缀数组多次不重叠子串)