# Fleet of the Eternal Throne

HDU 6138 (AC自动机) 2017ACM暑期多校联合训练 - Team 8 1006 Fleet of the Eternal Throne

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 664 Accepted Submission(s): 321

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.

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 105.

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

ac自动机，不会。哇哦哇哦

fail指针的含义：一个节点的失败指针就是指与这个节点上表示的字符相同，但是在树上的位置要是里这个节点最近的上面的那个节点。

#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
struct AC_Node///建立AC自动机的结构体
{
int nxt[26],fail,info,len;///当前节点的直接子节点、失败指针、每个节点的一个单独的信息、长度
void init()
{
fill(nxt,nxt+26,0);
fail=info=0;
}
} ac[N];
int id,q[N],pre;

void Insert(const string& s)
{
int now=0,len=s.size();
for(int i=0; i<len; i++)
{
int t=s[i]-'a';
if(ac[now].nxt[t])
now=ac[now].nxt[t];///如果当前的树里面已经有了这个节点
else
{
ac[++id].init();///创建一个新的节点
ac[now].nxt[t]=id;///当前节点的第t个子节点指向这个节点
now=id;///指针往下移动
}
ac[now].info=++pre;
ac[now].len=i+1;///长度
}
}

void Build_fail()///构建树上的每一个节点的失败指针
{
q[tail++]=0;
ac[0].fail=0;///根节点的失败指针肯定是指向自己的
{
for(int i=0; i<26; i++)
if(ac[now].nxt[i])
{
for(t=ac[now].fail; t&&!ac[t].nxt[i]; t=ac[t].fail);///t!=0&&a[t].nxt[i]==0时循环

if(now)
ac[ac[now].nxt[i]].fail=ac[t].nxt[i];
q[tail++]=ac[now].nxt[i];
}
}
}

int ans;
set<int>se;
string s;

vector<string>v;

void Search(int x,bool op)
{
int len=v[x].size(),now=0,t,k;
for(int i=0; i<len; i++)
{
t=v[x][i]-'a';
for(; now&&!ac[now].nxt[t]; now=ac[now].fail);///now!=0&&a[now].nxt[t]!=0
now=ac[now].nxt[t];
for(k=now; k; k=ac[k].fail)///在插进去这个字符串的同时也把这个串上涉及到的所有的失败节点插进去了
if(ac[k].info)
{
if(op)
{
se.insert(ac[k].info);
}
else
{
if(se.find(ac[k].info)!=se.end())
ans=max(ans,ac[k].len);
}
}
}
}

int main()
{
int T,n,m,x,y;
scanf("%d",&T);
while(T--)
{
v.clear();///vector容器
id=pre=0;
ac[0].init();///树的根节点的一个初始化过程
scanf("%d",&n);
for(int i=0; i<n; i++)
{
cin>>s;
v.push_back(s);
Insert(s);
}
Build_fail();
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&x,&y);
x--,y--;
ans=0;
se.clear();
Search(x,true);
Search(y,false);
printf("%d\n",ans);
}
}
return 0;
}

08-18 194