码迷,mamicode.com
首页 > 其他好文 > 详细

板子3

时间:2017-09-14 23:51:07      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:name   code   size   cst   cos   ace   har   while   emc   

  后缀自动机

/** @xigua */
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <climits>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 250005 + 5;
const int mod = 1e9 + 7;
const int INF = 1e8 + 5;
const ll inf = 1e15 + 5;
const db eps = 1e-6;
char s[maxn];
int Min[maxn<<1], Max[maxn<<1];

    int ch[maxn<<1][26];
    int fa[maxn<<1];
    int len[maxn<<1];
    int cnt, last;
    void init() {
        memset(ch, 0, sizeof(ch));
        memset(fa, 0, sizeof(fa));
        last=cnt=1;
    }
    void add(int c) {
        int p = last, np = last = ++cnt;
        len[np] = len[p] + 1;
        while(!ch[p][c] && p) {
            ch[p][c] = np;
            p = fa[p];
        }
        if(p == 0) fa[np] = 1;
        else {
            int q = ch[p][c];
            if(len[p] + 1 == len[q]) {
                fa[np] = q;
            } else {
                int nq = ++cnt;
                len[nq] = len[p] + 1;
                memcpy(ch[nq], ch[q], sizeof ch[q]);
                fa[nq] = fa[q];
                fa[q] = fa[np] = nq;
                while(ch[p][c] == q && p) {
                    ch[p][c] = nq;
                    p = fa[p];
                }
            }
        }
    }

void solve() {
    scanf("%s", s);
    int lenn = strlen(s);
    init();
    for(int i = 0; i < lenn; i++) {
        add(s[i] - a);
    }
    scanf("%s", s);
    lenn = strlen(s);
    int p = 1;
    int ans = 0;
    int c = 0;
    for(int i = 0; i < lenn; i++) {
        if(ch[p][s[i] - a]) {
            p = ch[p][s[i] - a];
            c++;
        }
        else {
            while(p&&!ch[p][s[i]-a])  p=fa[p];
            if (!p)  c=0, p=1;
            else  c=len[p]+1, p=ch[p][s[i]-a];
        }
        ans = max(ans, c);
    }
    printf("%d\n", ans);
}
int main() {
    int t = 1;
    //freopen("in.txt", "r", stdin);
  //  scanf("%d", &t);
    while(t--)
        solve();
    return 0;
}

 

板子3

标签:name   code   size   cst   cos   ace   har   while   emc   

原文地址:http://www.cnblogs.com/gggyt/p/7523317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!