Palisection(Codeforces Beta Round #17E+回文树)

题目链接

传送门

题意

给你一个串串,问你有多少对回文串相交。

思路

由于正着做不太好算答案,那么我们考虑用总的回文对数减去不相交的回文对数。

而不相交的回文对数可以通过计算以\(i\)为右端点的回文串的个数\(\times\)\(i+1,i+2\dots,n\)为左端点的回文串的个数计算得到。

\(i\)为右端点的回文串的个数可以直接用回文树\(O(n)\)求出来,以\(i\)为左端点的回文串的个数反着求一遍即可。

不过要注意本题由于数据范围比较大,使用邻接矩阵会导致\(MLE\),因此需要使用邻接矩阵来代替。

代码实现如下

#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;
}
posted @ 2019-07-29 10:48  Dillonh  阅读(226)  评论(0编辑  收藏  举报