hdu 6138 ac自动机

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:   6160  6159  6158  6157  6156 


题意:给出n个串,然后进行询问,给出x,y,问既是x,y的公共子串又是其他串的前缀串的最长串

思路:首先把n个串插入ac自动机,然后每次询问查找一下x串并把它染色(不只是然x串本身,还有前缀串是x子串的串),随后查找y,找到y也经过的然色的最长串,这里我们还要记录一下每个节点到根节点的距离(在插入的时候记录就可以了),这样我们找到的串就是既是x,y的子串又是其他串的前缀串了

ac代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e6 + 7;
const int maxnode = 50*10010;
int n;
char s1[60], s2[maxn];
int pos[maxn];
struct node
{
    int ch[maxnode][26], cnt[maxnode], fail[maxnode], last[maxnode], id, road[maxnode];
    int dep[maxnode], flag[maxnode];
    void init()
    {
        memset(ch[0], 0, sizeof(ch[0]));
        memset(dep, 0, sizeof(dep));
        id = 1;
    }
    void Insert(char *s)
    {
        int rt = 0;
        int len = strlen(s);
        dep[rt] = 0;
        for(int i = 0; i < len; i++)
        {
            if(!ch[rt][s[i]-'a'])
            {
                memset(ch[id], 0, sizeof(ch[id]));
                flag[id] = 0;
                ch[rt][s[i]-'a'] = id++;
            }
            dep[ch[rt][s[i]-'a']] = dep[rt] + 1;
            rt = ch[rt][s[i]-'a'];
        }
    }
    void get_fail()
    {
        queue<int> q;
        fail[0] = 0;
        int rt = 0;
        for(int i = 0; i < 26; i++)
        {
            rt = ch[0][i];
            if(rt)
            {
                q.push(rt);
                fail[rt] = 0;
            }
        }
        while(!q.empty())
        {
            int r = q.front();
            q.pop();
            for(int i = 0; i < 26; i++)
            {
                rt = ch[r][i];
                if(!rt)
                {
                    ch[r][i] = ch[fail[r]][i];
                    continue;
                }
                q.push(rt);
                fail[ch[r][i]] = ch[fail[r]][i];
            }
        }
    }
    void Match(char *s, int f)
    {
        int rt = 0;
        int len = strlen(s);
        for(int i = 0; i < len; i++)
        {
            int temp = rt = ch[rt][s[i]-'a'];
            while(temp)
            {
                flag[temp] = f;
                temp = fail[temp];
            }
        }
    }
    int Search(char *s, int f)
    {
        int rt = 0, res = 0;
        int len = strlen(s);
        for(int i = 0; i < len; i++)
        {
            int temp = rt = ch[rt][s[i]-'a'];
            while(temp)
            {
                if(flag[temp] == f) res = max(res, dep[temp]);
                temp = fail[temp];
            }
        }
        return res;
    }
}ac_auto;
char s[maxn];
int main()
{
    int t, q;
    cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        int d = 0;
        ac_auto.init();
        for(int i = 0; i < n; i++)
        {
            pos[i] = d;
            scanf(" %s", s+d);
            ac_auto.Insert(s+d);
            int len = strlen(s+d);
            d += len + 1;
        }
        ac_auto.get_fail();
        scanf("%d", &q);
        int ca = 1;
        while(q--)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            x--; y--;
            ac_auto.Match(s+pos[x],ca);
            int ans = ac_auto.Search(s+pos[y], ca++);
            printf("%d\n", ans);
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值