HDU 6138 Fleet of the Eternal Throne (AC自动机)

来源:互联网 发布:淘宝发货后立即回款 编辑:程序博客网 时间:2024/06/06 09:59

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138

题意:给出n个串以及m个询问,对于每个询问(x, y),回答串x和串y的最长公共连续子串,并且这个串还是n个串中任意一个串的前缀。

思路:将这n个串建立AC自动机,每个节点保存其到根节点的距离(即前缀的长度)。首先将串x在自动机上跑一遍,将经过的节点做个标记(表示串x可以匹配到这些节点所代表的前缀),再把串y在自动机上跑一遍,若其经过的节点已经存在标记,则说明这个节点所代表的前缀既能匹配x也能匹配y,更新最大答案即可。


#include<cstdio>#include<cstring>#include<string>#include<cctype>#include<iostream>#include<set>#include<map>#include<cmath>#include<sstream>#include<vector>#include<stack>#include<queue>#include<algorithm>#define fin freopen("1006.in","r",stdin)#define fout freopen("a.txt","w",stdout)typedef long long LL;using namespace std;typedef pair<int, int> P;const int INF = 1e8 + 10;const int maxn = 1e5 + 10;const int maxnode = 1e5 + 10;const int sigma_size = 26;string text[maxn];char in[maxn];int vis, ans;struct AC{int ch[maxnode][sigma_size];int val[maxnode];int f[maxnode];int last[maxnode];int flag[maxnode];int sz;void init() { sz = 1; memset(ch[0], 0, sizeof ch[0]); }int idx(char c) { return c - 'a'; }void insert(string &s){int u = 0, n = s.size();for(int i = 0; i < n; i++){int c = idx(s[i]);if(!ch[u][c]){memset(ch[sz], 0, sizeof ch[sz]);val[sz] = i+1;ch[u][c] = sz++;}u = ch[u][c];}}    int getFail()    {    queue<int> q;    f[0] = 0;    for(int c = 0; c < sigma_size; c++)    {    int u = ch[0][c];    if(u) { f[u] = 0; q.push(u); last[u] = 0; }    }    while(!q.empty())    {            int r = q.front(); q.pop();            for(int c = 0; c < sigma_size; c++)            {            int u = ch[r][c];            if(!u) { ch[r][c] = ch[f[r]][c]; continue; }            q.push(u);            int v = f[r];            while(v && !ch[v][c]) v = f[v];            f[u] = ch[v][c];            last[u] = val[f[u]] ? f[u] : last[f[u]];            }    }    }    void print1(int j)    {        if(!j) return;    if(flag[j] != vis) flag[j] = vis;    print1(last[j]);    }     void print2(int j)    {        if(!j) return;    if(flag[j] == vis) ans = max(ans, val[j]);    print2(last[j]);    }    void Find1(string &T)    {    int n = T.length();    int j = 0;    for(int i = 0; i < n; i++)    {    int c = idx(T[i]);    j = ch[j][c];            if(val[j]) print1(j);            else if(last[j]) print1(last[j]);    }    }    void Find2(string &T)    {        int n = T.length();    int j = 0;    for(int i = 0; i < n; i++)    {    int c = idx(T[i]);    j = ch[j][c];            if(val[j]) print2(j);            else if(last[j]) print2(last[j]);    }    }}ac;int main(){//fin; fout;int T;scanf("%d", &T);while(T--){ac.init();int n, m, x, y;scanf("%d", &n);for(int i = 1; i <= n; i++) { scanf("%s", in); text[i] = in; ac.insert(text[i]);}    ac.getFail();    scanf("%d", &m);while(m--){ans = 0;++vis;            scanf("%d%d", &x, &y);            ac.Find1(text[x]); ac.Find2(text[y]);            printf("%d\n", ans);}}return 0;}

Fleet of the Eternal Throne

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


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 (xy), 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 (n105).

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 (1m100) 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
13aaabaaacaaa22 31 2
 

Sample Output
33
 

Source
2017 Multi-University Training Contest - Team 8
 

Recommend
liuyiding
 


阅读全文
0 0
原创粉丝点击