HUST1718(ac自动机)

该博客讲述了如何解决一个字符串问题,即给定一个主键(master key)和多个仆从键(servant keys),找出每个仆从键在主键中最长的公共前缀长度。通过构建AC自动机并利用其进行匹配,可以找到每个仆从键的最长匹配前缀。样例输入和输出展示了具体操作过程。
摘要由CSDN通过智能技术生成

1718 - Key

Time Limit: 2s Memory Limit: 256MB
Submissions: 114 Solved: 9
DESCRIPTION
A key is a string consisting of ‘E’, ‘S’, ‘W’ and ‘N’s. Some of keys are called master keys, and the others are called servant keys. Give you one master key KM and N servant keys (S1 to SN). You are asked to determine what the length of the longest prefix which can be found in the master key is for every servant key.
INPUT
The first line of the input gives the number of test cases T. T test cases follow.
Each test case starts with a line consist of one string representing KM. The next line consists of one integer N. Then N lines follows. The ith line consists of one string representing Si.
1 ≤ |KM| ≤ 106.
1 ≤ N ≤ 104.
1 ≤ |Si| ≤ 102.
KM and Si consists of only ‘E’, ‘S’, ‘W’, and ‘N’.
1 ≤ the total length of all keys in one input file ≤ 4 × 106.
OUTPUT
For each test case, first output one line containing “Case #x:”, where x is the test case number.
Then for every servant key, output one line consists of one integer indicating the length of the longest prefix which can be found in the master key.
SAMPLE INPUT
2
SNNSSNS
3
NNSS
NNN
WSEE
NSWWEEW
3
NNSW
EEW
WWEE
SAMPLE OUTPUT
Case #1:
4
2
0
Case #2:
1
3
4
HINT
SOURCE
lizhen

题意:给你一个字符串KM,然后给你n个字符串,问你这n个字符串中的每一个串能在KM串中找到一个最长的匹配前缀是多长。

解题思路:对于这个n个字符串建立ac自动机,然后与KM串进行匹配,匹配过程中,给能匹配上的节点打上标记就行,然后每个串能够匹配上的最长前缀就是,从根节点到该节点路径上的最远的那个打上标记的长度。

#include<bits/stdc++.h>
using namespace std;
int N;
char KM[1000005];
char S[10005][105];
struct node{
    int d;
    int h;
    node* Next[4];
    node* fail;
    bool flag;//
    bool visit;
};
node LiZhiMing[1000005];
node* re[10005];
int cnt;
node* root;
inline node* getV()
{
    LiZhiMing[cnt].d = 0;
    LiZhiMing[cnt].fail = NULL;
    LiZhiMing[cnt].flag = false;
    LiZhiMing[cnt].h = 0;
    LiZhiMing[cnt].visit = false;
    memset(LiZhiMing[cnt].Next, 0, sizeof(LiZhiMing[cnt].Next));
    return &LiZhiMing[cnt++];
}
void Insert(char *s,int id)
{
    node* p = root;
    for(int i = 0; s[i]; i++)
    {
        int num;
        if(s[i] == 'E') num = 0;
        else if(s[i] == 'S') num = 1;
        else if(s[i] == 'W') num = 2;
        else num = 3;
        if(p->Next[num] == NULL)
        {
            node* res = getV();
            p->Next[num] = res;
        }
        p->Next[num]->d = i + 1;
        p = p->Next[num];
    }
    re[id] = p;
}
void bfs()
{
    queue<node*> Q;
    while(!Q.empty()) Q.pop();
    Q.push(root);
    node* nx;
    while(!Q.empty())
    {
        nx = Q.front();
        Q.pop();
        int h = nx->h;
        for(int i = 0; i < 4; i++)
        {
            if(nx->Next[i])
            {
                if(nx->Next[i]->flag)
                {
                    nx->Next[i]->h = nx->Next[i]->d;
                }
                else
                {
                    nx->Next[i]->h = h;
                }
                Q.push(nx->Next[i]);
            }
        }

    }

}
//void Search(char *s, int id)
//{
//    node* p = root;
//    for(int i = 0; s[i]; i++)
//    {
//        int num;
//        if(s[i] == 'E') num = 0;
//        else if(s[i] == 'S') num = 1;
//        else if(s[i] == 'W') num = 2;
//        else num = 3;
//        if(p->Next[num]->flag)
//        {
//            dp[id] = max(dp[id], p->Next[num]->d);
//        }
//        else break;
//        p = p->Next[num];
//    }
//}
void build_fail()
{
    node* p = root;
    node* ans;
    node* nx;
    queue<node*> Q;
    Q.push(p);
    while(!Q.empty())
    {
        nx = Q.front(); Q.pop();
        for(int i = 0; i < 4; i++)
        {
            if(nx->Next[i])
            {
                if(nx == root)
                {
                    nx->Next[i]->fail = root;
                }
                else
                {
                    ans = nx->fail;
                    while(ans && ans->Next[i] == NULL)
                    {
                        ans = ans->fail;
                    }
                    if(ans == NULL) nx->Next[i]->fail = root;
                    else nx->Next[i]->fail = ans->Next[i];
                }
                Q.push(nx->Next[i]);
            }
        }
    }
}
void ac_automation(char *s)
{
    node* p = root;
    node* res;
    for(int i = 0; s[i]; i++)
    {
        int num;
        if(s[i] == 'E') num = 0;
        else if(s[i] == 'S') num = 1;
        else if(s[i] == 'W') num = 2;
        else num = 3;
        if(p->Next[num] == NULL)
        {
            while(p && p->Next[num] == NULL)
            {
                p = p->fail;
            }
            if(p == NULL) p = root;
            else
            {
                p = p->Next[num];
                res = p;
                while(res != root && !res->visit)
                {
                    res->flag = true;
                    res->visit = true;
                    res = res->fail;
                }
            }
        }
        else
        {
                p = p->Next[num];
                res = p;
                while(res != root && !res->visit)
                {
                    res->flag = true;
                    res->visit = true;
                    res = res->fail;
                }
        }
    }
}
int main()
{
//    freopen("C:\\Users\\creator\\Desktop\\in.txt","r",stdin ) ;
//    freopen("C:\\Users\\creator\\Desktop\\out.txt","w",stdout ) ;

    int T;
    scanf("%d", &T);
    int Ca = 1;
    while(T--)
    {
        cnt = 0;
        scanf("%s", KM);
        scanf("%d", &N);
        root = getV();
        root->d = 0;
        root->h = 0;
        root->flag = true;
        for(int i = 1; i <= N; i++)
        {
            scanf("%s", S[i]);
            Insert(S[i], i);
        }
        build_fail();
        ac_automation(KM);
        bfs();
//        for(int i = 1; i <= N; i++)
//        {
//            Search(S[i], i);
//        }
        printf("Case #%d:\n", Ca++);
        for(int i = 1; i <= N; i++)
        {
            printf("%d\n", re[i]->h);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值