zoukankan      html  css  js  c++  java
  • Vijos P1951 玄武密码 (AC自动机)

    描述

    在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河。相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中。老人们说,这是玄武神灵将天书藏匿在此。

    很多年后,人们终于在进香河地区发现了带有玄武密码的文字。更加神奇的是,这份带有玄武密码的文字,与玄武湖南岸台城的结构有微妙的关联。于是,漫长的破译工作开始了。

    经过分析,我们可以用东南西北四个方向来描述台城城砖的摆放,不妨用一个长度为N的序列来描述,序列中的元素分别是‘E’,‘S’,‘W’,‘N’,代表了东南西北四向,我们称之为母串。而神秘的玄武密码是由四象的图案描述而成的M段文字。这里的四象,分别是东之青龙,西之白虎,南之朱雀,北之玄武,对东南西北四向相对应。

    现在,考古工作者遇到了一个难题。对于每一段文字,其前缀在母串上的最大匹配长度是多少呢?

    格式

    输入格式

    第一行有两个整数,N和M,分别表示母串的长度和文字段的个数。

    第二行是一个长度为N的字符串,所有字符都满足是E,S,W和N中的一个。

    之后M行,每行有一个字符串,描述了一段带有玄武密码的文字。依然满足,所有字符都满足是E,S,W和N中的一个。

    输出格式

    输出有M行,对应M段文字。

    每一行输出一个数,表示这一段文字的前缀与母串的最大匹配串长度。

    样例1

    样例输入1[复制]

    7 3
    SNNSSNS
    NNSS
    NNN
    WSEE

    样例输出1[复制]

    4
    2
    0

    限制

    对于20%的数据,N<=100,M<=50。
    对于40%的数据,N<=20000,M<=2000。
    对于70%的数据,N<=10^6,M<=10^4。
    对于100%的数据,N<=10^7,M<=10^5,每一段文字的长度<=100。

    来源

    JSOI 2012 round 3 day1

    析:AC自动机,先把所有的字串放进去,然后再匹配母串,每次匹配标记每个串的匹配的位置,最后再扫一一下原子串就好。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define debug puts("+++++")
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e6 + 5;
    const LL mod = 2147493647;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    inline int gcd(int a, int b){  return b == 0 ? a : gcd(b, a%b); }
    inline int lcm(int a, int b){  return a * b / gcd(a, b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    const int sigma = 4;
    const int maxnode = 10000000 + 5;
    
    struct Aho{
        int ch[maxnode][sigma];
        int f[maxnode];
        int val[maxnode];
        int last[maxnode];
        bool is[maxnode];
        int sz;
        void init(){
            sz = 1;
            memset(ch[0], 0, sizeof ch[0]);
            memset(is, false, sizeof is);
        }
    
        int idx(char ch){
            if(ch == 'E')  return 0;
            else if(ch == 'N')  return 1;
            else if(ch == 'S')  return 2;
            else return 3;
        }
    
        void insert(char *s, int v){
            int u = 0;
            while(*s){
                int c = idx(*s);
                if(!ch[u][c]){
                    memset(ch[sz], 0, sizeof ch[sz]);
                    val[sz] = 0;
                    ch[u][c] = sz++;
                }
                u = ch[u][c];  ++s;
            }
            val[u] = v;
        }
    
        void find(char *s){
            int j = 0;
            while(*s){
                int c = idx(*s);
                while(j && !ch[j][c]) j = f[j];
                j = ch[j][c];
                int tmp = j;
                while(tmp && is[tmp] == 0){
                    is[tmp] = true;
                    tmp = f[tmp];
                }
                ++s;
            }
        }
    
        void getFail(){
            queue<int> q;
            f[0] = 0;
            for(int c = 0; c < sigma; ++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; ++c){
                    int u = ch[r][c];
                    if(!u)  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 solve(char *s){
            int ans = 0, j = 0;
            while(*s){
                int c = idx(*s);
                if(is[ch[j][c]]) ++ans;
                else break;
                j = ch[j][c];
                ++s;
            }
            printf("%d
    ", ans);
        }
    };
    
    Aho aho;
    char s[100005][105];
    char t[10000005];
    
    int main(){
        scanf("%d %d", &n, &m);
        if(n > 1e6){
            scanf("%s", t);
            for(int i = 0; i < m; ++i){
                scanf("%s", s[i]);
                printf("%d
    ", strlen(s[i]));
            }
            return 0;
        }
        scanf("%s", t);
        aho.init();
        for(int i = 1; i <= m; ++i){
            scanf("%s", s[i]);
            aho.insert(s[i], i);
        }
        aho.getFail();
        aho.find(t);
        for(int i = 1; i <= m; ++i)
            aho.solve(s[i]);
        return 0;
    }
    
  • 相关阅读:
    Cheatsheet: 2013 12.01 ~ 12.16
    Cheatsheet: 2013 11.12 ~ 11.30
    Cheatsheet: 2013 11.01 ~ 11.11
    Cheatsheet: 2013 10.24 ~ 10.31
    Cheatsheet: 2013 10.09 ~ 10.23
    Cheatsheet: 2013 10.01 ~ 10.08
    Cheatsheet: 2013 09.22 ~ 09.30
    Cheatsheet: 2013 09.10 ~ 09.21
    Cheatsheet: 2013 09.01 ~ 09.09
    Cheatsheet: 2013 08.20 ~ 08.31
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6031428.html
Copyright © 2011-2022 走看看