Palisection (Codeforces Beta Round # 17E + palindrome tree)

Topic Link

Portal

The meaning of problems

Give you a string, you have to ask how much of the palindrome string intersect.

Thinking

Due to the positive answers do not very good count, then we consider to minus disjoint palindrome palindrome of the total number.

Not to intersect palindrome can be calculated in \ (I \) a palindromic sequence number of the right end point \ (\ Times \) to \ (i + 1, i + 2 \ n dots, \) of palindromic sequence number of the left point is calculated.

In \ (I \) the number of the right end point of the palindromic sequence may be directly palindromic tree \ (O (n) \) seek out to \ (I \) the number of the left end point of the palindromic sequence counter and begged it again.

But pay attention to this problem due to the relatively large range of data, using the adjacency matrix will lead to \ (MLE \) , it is necessary to use an adjacency matrix instead.

Code to achieve the following

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 51123987;
const int maxn = 2e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n;
LL ans, tmp;
LL sum[maxn];
char s[maxn];

struct PAM {
    //len数组表示以i为结尾的最长回文子串长度
    //tot为结点数,lst为上一个字符加的位置
    int N, totedge;
    int str[maxn], head[maxn];
    int fail[maxn], len[maxn], cnt[maxn], num[maxn], tot, lst;
    void init() {
        for(int i = 0; i <= n + 1; ++i) {
            head[i] = -1;
            cnt[i] = len[i] = fail[i] = num[i] = 0;
        }
        totedge = N = lst = 0; tot = 1; fail[0] = fail[1] = 1; len[1] = -1;
    }

    struct edge {
        int v, w, next;
    }ed[maxn];

    void add(int u, int v, int w) {
        ed[totedge].v = v;
        ed[totedge].w = w;
        ed[totedge].next = head[u];
        head[u] = totedge++;
    }

    inline int fi(int u, int v) {
        for(int i = head[u]; ~i; i = ed[i].next) {
            if(ed[i].v == v) return ed[i].w;
        }
        return 0;
    }

    inline int add(int c) {
        int p = lst;
        str[++N] = c;
        while(str[N - len[p] - 1] != str[N]) p = fail[p];
        if(!(lst = fi(p, c))) {
            int now = ++tot, k = fail[p];
            len[now] = len[p] + 2;
            while(str[N - len[k] - 1] != str[N]) k = fail[k];
            fail[now] = fi(k, c);
            add(p, c, now);
            num[now] = num[fail[now]] + 1;
            lst = now;
        }
        cnt[lst]++;
        return num[lst];
    }
    inline void solve() {
        for(int i = tot; i; i--) {
            cnt[fail[i]] += cnt[i];
            (tmp += cnt[i]) %= mod;
        }
    }
}pam;

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    scanf("%d", &n);
    scanf("%s", s + 1);
    pam.init();
    for(int i = n; i >= 1; --i) {
        sum[i] = (sum[i+1] + pam.add(s[i] - 'a' + 1)) % mod;
    }
    pam.init();
    for(int i = 1; i <= n; ++i) {
        int cnt = pam.add(s[i] - 'a' + 1);
        (ans += 1LL * cnt * sum[i+1] % mod) %= mod;
    }
    pam.solve();
    printf("%lld\n", (((1LL * tmp * (tmp-1) / 2 % mod) - ans) % mod + mod) % mod);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Dillonh/p/11262638.html