G - Fleet of the Eternal Throne HDU - 6138 —— ac自动机

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

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
题意:给你n个字符串,m个询问,问你x,y最大相同子串并且这个子串是任意一个字符串的前缀。
模板题,设一个走过的标记和深度,再次走到的时候记录最大深度,
就相当于你找第一个字符串的时候把所有包含的字符串找到了,第二次找的时候找到的必然是某一个字符串的前缀。

#include<bits/stdc++.h>
using namespace std;

struct Tire{
    static const int NODENUM=(int)1e6+5,R=26;
    int nxt[NODENUM][R],fail[NODENUM],flag[NODENUM],dep[NODENUM];

    int rt,tot;
    int newnode(){
        for(int i=0;i<R;i++)nxt[tot][i]=-1;
        flag[tot]=0;
        return tot++;
    }
    void init(){
        tot=0;
        rt=newnode();
        memset(dep,0,sizeof(dep));
    }
    void insert(char *s){
        dep[rt]=0;
        int now=rt,len=strlen(s);
        for(int i=0;i<len;i++){
            int val=s[i]-'a';
            if(nxt[now][val]==-1)nxt[now][val]=newnode();
            dep[nxt[now][val]]=dep[now]+1;
            now=nxt[now][val];
        }
    }
    void build(){
        queue<int>q;
        fail[rt]=rt;
        for(int i=0;i<R;i++){
            if(nxt[rt][i]==-1)nxt[rt][i]=rt;
            else {
                fail[nxt[rt][i]]=rt;
                q.push(nxt[rt][i]);
            }
        }
        while(!q.empty()){
            int now=q.front();q.pop();
            for(int i=0;i<R;i++){
                if(nxt[now][i]==-1)nxt[now][i]=nxt[fail[now]][i];
                else {
                    fail[nxt[now][i]]=nxt[fail[now]][i];
                    q.push(nxt[now][i]);
                }
            }
        }
    }
    void query1(char *s,int f){
        int now=rt,res=0,len=strlen(s);
        for(int i=0;i<len;i++){
            int val=s[i]-'a';
            now=nxt[now][val];
            int tmp=now;
            while(tmp!=rt){
                flag[tmp]=f;
                tmp=fail[tmp];
            }
        }
    }
    int query2(char *s,int f){
        int now=rt,res=0,len=strlen(s);
        for(int i=0;i<len;i++){
            int val=s[i]-'a';
            now=nxt[now][val];
            int tmp=now;
            while(tmp!=rt){
                if(flag[tmp]==f)
                    res=max(res,dep[tmp]);
                tmp=fail[tmp];
            }
        }
        return res;
    }
}ac;

char s[1000005];
int pos[1000005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ac.init();
        int n;
        scanf("%d",&n);
        int pp=0;
        for(int i=1;i<=n;i++)
        {
            pos[i]=pp;
            scanf("%s",s+pp);
            ac.insert(s+pp);
            pp+=strlen(s+pp)+1;
        }
        ac.build();
        int m;
        scanf("%d",&m);
        int x,y;
        int num=0;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            num++;
            ac.query1(s+pos[x],num);
            printf("%d\n",ac.query2(s+pos[y],num));
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值