字符串模版

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5+10;
char s[maxn], t[maxn];
int nxt[maxn], ext[maxn], f[maxn], p[maxn];

//kmp
void getNext(char *t) {
    nxt[0] = 0;
    for (int i=1, j=0; t[i]; ++i) {
        while(j && t[i]!=t[j]) j=nxt[j-1];
        if(t[i]==t[j]) ++j;
        nxt[i] = j;
    }
}
bool kmp(char *s, char *t) {
    getNext(t);
    bool fg = false, l = strlen(t);
    for (int i=0, j=0; s[i]; ++i) {
        while(j && s[i]!=t[j]) j=nxt[j-1];
        if(s[i]==t[j]) ++j;
        f[i] = j;
        if(j==l) fg = 1;
    }
    return fg;
}

//exkmp
void getNext(char *t, int m) {
    int a=0, p=0;
    nxt[0] = m;
    for (int i=1; i<m; ++i) {
        if(i>=p || i+nxt[i-a]>=p) {
            if(i>=p) p=i;
            while(p<m && t[p]==t[p-i]) ++p;
            nxt[i]=p-i;
            a=i;
        } else {
            nxt[i] = nxt[i-a];
        }
    }
}
void getExt(char *s, int n, char *t, int m) {
    getNext(t, m);
    int a=0, p=0;
    for (int i=0; i<n; ++i) {
        if(i>=p || i+nxt[i-a]>=p) {
            if(i>=p) p=i;
            while(p<n && p-i<m && s[p]==t[p-i]) ++p;
            ext[i] = p-i;
            a=i;
        } else {
            ext[i] = nxt[i-a];
        }
    }
}

// manacher
char s[maxn], ms[maxn];
inline void init(char *s) {
    ms[0] = '@', ms[1] = '#';
    for (int i = 1; s[i]; ++i)
        ms[i<<1] = s[i], ms[i<<1|1] = '#';
    int l = strlen(s+1);
    ms[l*2+2] = '^', ms[l*2+3] = 0;
}

inline void manacher(char *s) {
    init(s);
    int id = 0, mx = 0;
    int l = strlen(ms);
    for (int i = 1; i < l; ++i) {
        p[i] = mx>i? min(mx-i, p[2*id-i]): 1;
        while(p[i]+i < l && ms[i+p[i]] == ms[i-p[i]]) ++p[i];
        if(p[i] + i > mx) {
            mx = p[i] + i;
            id = i;
        }
    }
}

int main() {

    return 0;
}

//PAM
#include <bits/stdc++.h>
#include "utility"

using namespace std;
const int maxn = 3e5+10;
const int maxm = maxn*26;

struct Pam {
    int len[maxm], cnt[maxm], num[maxn];
    int nxt[maxn][30], fail[maxm], s[maxn];
    int p, last, n;

    int insert(int l) {
        for (int i=0; i<26; ++i) nxt[p][i]=0;
        cnt[p]=fail[p]=0;
        len[p]=l;
        return p++;
    }

    void init() {
        p = n = last = 0;
        insert(0); insert(-1);
        s[n] = -1;
        fail[0]=1;
    }

    int getFail(int x) {
        while(s[n-len[x]-1]!=s[n]) x=fail[x];
        return x;
    }

    void add(int c) {
        c-='a';
        s[++n] = c;
        int cur = getFail(last);
        if(!nxt[cur][c]) {
            int now = insert(len[cur]+2);
            fail[now] = nxt[getFail(fail[cur])][c];
            nxt[cur][c] = now;
            num[now] = num[fail[now]]+1;
        }
        last = nxt[cur][c];
        ++cnt[last];
    }

    void count() {
        for (int i=p-1; i>=0; --i)
            cnt[fail[i]] += cnt[i];
    }
}pam;

char str[maxn];

int main() {
    pam.init();
    scanf("%s", str);
    for (int i=0; str[i]; ++i) pam.add(str[i]);
    pam.count();

    return 0;
}
posted @ 2019-08-05 23:31  Acerkoo  阅读(166)  评论(0编辑  收藏  举报