2019牛客多校第四场 I-string (SA+PAM)

传送门

用后缀数组处理出S#rev(S)的不同子串个数P,减去包含了#的子串个数,这样非回文串的个数就被计算了两遍,而回文串个数只计算了一遍,因此再用回文自动机处理出S的本质不同的回文子串个数Q,答案就是(P+Q)/2

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int nMax = 4e5+5;
char s1[nMax]; char s2[nMax];
char str[nMax];
int sa[nMax], rk[nMax], h[nMax];
int wa[nMax], wb[nMax], wv[nMax], c[nMax], vis[1005], id[nMax];

int cmp(int *r, int a, int b, int l){
    return r[a] == r[b] && r[a+l] == r[b+l];
}

void get_SA(char *r, int n, int m){          //  倍增算法 r为待匹配数组  n为总长度 m为字符范围
    int i, j, p, *x = wa, *y = wb, *t;
    for(i = 0; i < m; i ++) c[i] = 0;
    for(i = 0; i < n; i ++) c[x[i]=r[i]] ++;
    for(i = 1; i < m; i ++) c[i] += c[i-1];
    for(i = n-1; i >= 0; i --) sa[-- c[x[i]]] = i;
    for(j = 1, p = 1; p < n; j *= 2, m = p){
        for(p = 0, i = n-j; i < n; i ++) y[p ++] = i;
        for(i = 0; i < n; i ++) if(sa[i] >= j) y[p ++] = sa[i] - j;
        for(i = 0; i < n; i ++) wv[i] = x[y[i]];
        for(i = 0; i < m; i ++) c[i] = 0;
        for(i = 0; i < n; i ++) c[wv[i]] ++;
        for(i = 1; i < m; i ++) c[i] += c[i-1];
        for(i = n-1; i >= 0; i --) sa[-- c[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i ++){
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1: p ++;
        }
    }
}

void get_Height(char *r, int n){           //  求height数组。
    int i, j, k = 0;
    for(i = 1; i <= n; i ++) rk[sa[i]] = i;
    for(i = 0; i < n; h[rk[i ++]] = k){
        for(k ? k -- : 0, j = sa[rk[i]-1]; r[i+k] == r[j+k]; k ++);
    }
}
const int MAXN = 600005 ;
const int N = 26 ;

struct Palindromic_Tree {
	int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
	int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
	int cnt[MAXN] ;
	int num[MAXN] ;
	int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
	int S[MAXN] ;//存放添加的字符
	int last ;//指向上一个字符所在的节点,方便下一次add
	int n ;//字符数组指针
	int p ;//节点指针

	int newnode ( int l ) {//新建节点
		for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
		cnt[p] = 0 ;
		num[p] = 0 ;
		len[p] = l ;
		return p ++ ;
	}

	void init () {//初始化
		p = 0 ;
		newnode (  0 ) ;
		newnode ( -1 ) ;
		last = 0 ;
		n = 0 ;
		S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
		fail[0] = 1 ;
	}

	int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的
		while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ;
		return x ;
	}

	void add ( int c ) {
		c -= 'a' ;
		S[++ n] = c ;
		int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置
		if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
			int now = newnode ( len[cur] + 2 ) ;//新建节点
			fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
			next[cur][c] = now ;
			num[now] = num[fail[now]] + 1 ;
		}
		last = next[cur][c] ;
		cnt[last] ++ ;
	}

	void count () {
		for ( int i = p - 1 ; i >= 0 ; -- i ) cnt[fail[i]] += cnt[i] ;
		//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
	}
} pat;

int main(){
    scanf("%s",s1);
    int n = strlen(s1);
    ll m = (ll)n;
    pat.init();
    for(int i = 0; i < n; i++)
        pat.add(s1[i]);
    int x1 = pat.p-2;
    s1[n] = '#';
    int l = 1;
    for(int i = n-1; i >= 0; i--)
        s1[n+l] = s1[i],l++;
    n <<= 1;
    n++;
    s1[n] = 0;
    get_SA(s1,n+1,255);
    get_Height(s1,n);
    ll cc = (ll)n;
    ll ans = (cc+1)*cc/2;
    for(int i = 2; i <= n; i++){
        ans -= h[i];
    }

    ans -= (m+1)*(m+1);

    printf("%lld\n",(ans+1ll*x1)/2);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值