Fleet of the Eternal Throne
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 461 Accepted Submission(s): 141
Problem Description
> The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet's builders is unknown but their legacy would live on. Its first known action was in the annihilation of all life in Wild Space. It spread across Wild Space and conquered almost every inhabited world within the region, including Zakuul. They were finally defeated by a mysterious vessel known as the Gravestone, a massive alien warship that countered the Eternal Fleet's might. Outfitted with specialized weapons designed to take out multiple targets at once, the Gravestone destroyed whole sections of the fleet with a single shot. The Eternal Fleet was finally defeated over Zakuul, where it was deactivated and hidden away. The Gravestone landed in the swamps of Zakuul, where the crew scuttled it and hid it away.
>
> — Wookieepedia
The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.
The battleships of the Eternal Fleet are placed on a 2D plane of n rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.
aa
bbbaaa
abbaababa
abba
If in the x-th row and the y-th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x-th row and y-th row), at the same time the substring is a prefix of any other row (can be the x-th or the y-th row), the Eternal Fleet will have a risk of causing chain reaction.
Given a query (x, y), you should find the longest substring that have a risk of causing chain reaction.
>
> — Wookieepedia
The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.
The battleships of the Eternal Fleet are placed on a 2D plane of n rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.
aa
bbbaaa
abbaababa
abba
If in the x-th row and the y-th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x-th row and y-th row), at the same time the substring is a prefix of any other row (can be the x-th or the y-th row), the Eternal Fleet will have a risk of causing chain reaction.
Given a query (x, y), you should find the longest substring that have a risk of causing chain reaction.
Input
The first line of the input contains an integer T, denoting the number of test cases.
For each test cases, the first line contains integer n (n≤10^5).
There are n lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 10^5.
And an integer m (1≤m≤100) is following, representing the number of queries.
For each of the following m lines, there are two integers x,y, denoting the query.
For each test cases, the first line contains integer n (n≤10^5).
There are n lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 10^5.
And an integer m (1≤m≤100) is following, representing the number of queries.
For each of the following m lines, there are two integers x,y, denoting the query.
Output
You should output the answers for the queries, one integer per line.
Sample Input
1 3 aaa baaa caaa 2 2 3 1 2
Sample Output
3 3
Source
2017 Multi-University Training Contest - Team 8 |
My Solution
题意:给出n(n<=1e5)个字符串,且字符串的字符总和<=1e5,给出m个询问,每次给定x,y,找出对于给定str[x]和str[y]
的公共子串且满足这个串是所有串中的某个串的前缀,要求所得的公共串的长度尽可能大。
后缀数组+字典树
首先用给出的n个字符串建立字典树,
然后对于每次的询问,把str[x]和str[y]用‘#’连起来然后跑出后缀数组,
对于所有的ind[i](其中sa[ind[i]] <= len, ind[i]为其在sa数组中的下标),然后对于 ind[i] - ind[i-1] > 1则说明ind[i]和ind[i-1]直接存在!(sa[j] <= len)的情况,此时的height[ind[i]]表示
他们的后缀的最大相同后缀也就是 str[x]与str[y]的一个子串,然后用字典树O(length)的跑出护符条件的最大长度。
同理对于所有的ind'[i](其中sa[ind[i]] >= len)也跑一遍,记录最大值,就是答案了。
时间复杂度 O(nlogn)
空间复杂度 O(sum_length)
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 8;
const int CHAR_SIZE = 26;
const int MAX_SIZE = 1e5 + 8;
struct AC_Machine{//这里只使用了字典树的功能,由于方便直接上了AC自动机的SCL
int ch[MAX_SIZE][CHAR_SIZE];//, danger[MAX_SIZE], fail[MAX_SIZE];
int sz, n, u, c;
void init(){
sz = 1;
memset(ch[0], 0, sizeof ch[0]);
//memset(danger, 0, sizeof danger);
}
void _insert(const string &s){
n = s.size();
u = 0;
for(int i = 0; i < n; i++){
c = s[i] - 'a';
if(!ch[u][c]){
memset(ch[sz], 0, sizeof ch[sz]);
//danger[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
//danger[u]++;
}
int _find(const string &s){
n = s.size();
u = 0;
for(int i = 0; i < n; i++){
c = s[i] - 'a';
if(!ch[u][c]){
return i;
}
u = ch[u][c];
}
return n;
}
}ac;
string str[MAXN];
const int maxn = 2e5 + 8;
int sa[maxn], height[maxn];
int _rank[maxn], t1[maxn], t2[maxn], c[maxn];
string s;
inline void get_sa(const int &n, int m)
{
int i, k, *x = t1, *y = t2, p, j;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) ++ c[x[i] = s[i]];
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[-- c[x[i]]] = i;
for(k = 1; k <= n; k <<= 1){
p = 0;
for(i = n - k; i < n; i++) y[p ++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p ++] = sa[i] - k;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) ++ c[x[y[i]]];
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y), p = 1, x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = (y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k]) ? p - 1 : p ++;
if(p >= n) break;
m = p;
}
k = 0;
for(i = 0; i < n; i++) _rank[sa[i]] = i;
for(i = 0; i < n; i++){
if(k) --k; if(!_rank[i]) continue;
j = sa[_rank[i] - 1];
while(s[i + k] == s[j + k]) k++;
height[_rank[i]] = k;
}
}
inline void print(const int &n)
{
for(int i = 1; i <= n; i++){
//cout << i << " : " << _rank[sa[i]] << " " << sa[i] << endl;
for(int j = sa[i]; j < n; j++){
cout << s[j];
}
cout << endl;
}
cout << endl;
}
int xind[MAXN];
int main()
{
#ifdef LOCAL
freopen("f.txt", "r", stdin);
//freopen("f.out", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
int T, n, m, i, j, szs, x, y, len1, last , now, ptr;
int l, r, mid, ans;
cin >> T;
while(T--){
cin >> n;
ac.init();
for(i = 1; i <= n; i++){
cin >> str[i];
ac._insert(str[i]);
}
cin >> m;
while(m--){
cin >> x >> y;
s.clear();
s += str[x];
s += '#';
s += str[y];
szs = s.size();
get_sa(szs+1, 256);
len1 = str[x].size();
last = 0; ans = 0; ptr = 0;
for(i = 0; i < szs; i++){
if(sa[i] >= len1){
xind[ptr] = i;
ptr++;
}
}
//print(szs);
for(i = 1; i < ptr; i++){
if(xind[i] - xind[i-1] > 1){
//cout << height[xind[i]] << "?" << endl;
if(ans < height[xind[i]]){
ans = max(ans, ac._find(s.substr(sa[xind[i]], height[xind[i]])));
}
}
}
ptr = 0;
for(i = 0; i < szs; i++){
if(sa[i] <= len1){
xind[ptr] = i;
ptr++;
}
}
//print(szs);
for(i = 1; i < ptr; i++){
if(xind[i] - xind[i-1] > 1){
//cout << height[xind[i]] << "?" << endl;
if(ans < height[xind[i]]){
ans = max(ans, ac._find(s.substr(sa[xind[i]], height[xind[i]])));
}
}
}
cout << ans << "\n";
}
}
return 0;
}
Thank you!
------from ProLights
来源:oschina
链接:https://my.oschina.net/u/4278251/blog/4462796