HDU 6138 Fleet of the Eternal Throne (2017多校8 - AC自动机)

题意:

给你n 个串, q个查询, 每个查询 输出串x 和 串y 的最大公共子串 且这个串 是其他某些串的前缀。 输出长度。

思路:

因为场上读错题, 以为是个极水的后缀数组。= =

其实是ac自动机

我们给n 个串建立ac自动机。

字典树中每个节点维护一个 到根的距离(即是前缀的长度)

然后给串x 跑一遍自动机, 把经过的点的标记出来, 表示 这个点即是 x 的子串, 又是某个串的前缀。

然后在给y 跑一遍自动机, 当经过标记点时 更新一下答案即可, 因为这时候 这个串 即是x 的子串又是y 的子串 又是前缀。 更新答案好了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;


const int maxn = 100000 + 10 << 1;

struct Trie{
    int len[maxn];
    int next[maxn][26];
    int fail[maxn];
    int flag[maxn];
    int root;
    int L;


    void init(){
        L = 0;
        root = newnode();
        len[root] = 0;
    }
    int newnode(){
        for (int i = 0; i < 26; ++i){
            next[L][i] = -1;
        }
        flag[L] = 0;
        return L++;
    }

    void insert(char* s){
        int nod = root;
        int lens = strlen(s);
        for (int i = 0; i < lens; ++i){
            int id = s[i] - 'a';
            if (next[nod][id] == -1){
                next[nod][id] = newnode();
            }
            len[next[nod][id] ] = len[nod] + 1;
            nod = next[nod][id];
        }
    }

    void build(){
        queue<int>q;
        fail[root] = root;

        for (int i = 0; i < 26; ++i){
            if (next[root][i] == -1){
                next[root][i] = root;

            }
            else {
                fail[next[root][i] ] = root;
                q.push(next[root][i]);
            }
        }
        while(!q.empty()){
            int u = q.front(); q.pop();

            for (int i = 0; i < 26; ++i){
                if (next[u][i] == -1){
                    next[u][i] = next[fail[u] ][i];

                }
                else {

                    fail[next[u][i] ] = next[fail[u] ][i];
                    q.push(next[u][i]);
                }
            }
        }
    }


    void update(char* s, int vis){
        int lens = strlen(s);
        int nod = root;
        for (int i = 0; i < lens; ++i){
            int id = s[i] - 'a';
            nod = next[nod][id];
            int tmp = nod;
            while(tmp != root){
                flag[tmp] = vis;
                tmp = fail[tmp];
            }
        }
    }
    int query(char* s, int vis){
        int lens = strlen(s);
        int nod = root;
        int ans = 0;
        for (int i = 0; i < lens; ++i){
            int id = s[i] - 'a';
            nod = next[nod][id];
            int tmp = nod;
            while(tmp != root){
                if (flag[tmp] == vis){
                    ans = max(ans, len[tmp]);
                }
                tmp = fail[tmp];
            }
        }
        return ans;

    }





}ac;


char s[maxn];
int d;

int pos[maxn];
int main(){

    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d", &n);
        d = 0;
        ac.init();
        for (int i = 0; i < n; ++i){
            pos[i] = d;
            scanf("%s", s + d);
            ac.insert(s + d);
            int len = strlen(s + d);
            d += len + 1;
        }
        ac.build();
        int q;
        scanf("%d", &q);
        int vis = 1;
        while(q--){
            int x, y;
            scanf("%d %d", &x, &y);
            --x;--y;
            ac.update(s + pos[x], vis);
            int ans = ac.query(s + pos[y], vis);
            ++vis;
            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): 173    Accepted Submission(s): 76


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  ( 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
  
1 3 aaa baaa caaa 2 2 3 1 2
 

Sample Output
  
3 3
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6143  6142  6141  6140  6139 
 

Statistic |  Submit |  Discuss |  Note

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页