Gym - 100548G (回文树,公共前缀的对数)

传送门

题意:给你两个字符串 s 1 , s 2 s1,s2 s1,s2,求它们的公共前缀有多少对

题解:对 s 1 s1 s1建一棵回文树,然后部分初始化,再在 s 1 s1 s1的基础上对 s 2 s2 s2建回文树。 然后对 c n t cnt cnt乘积求和

为什么可以这样?

因为第一次建树的时候求出了 s 1 s1 s1在每个节点不同的回文子串个数, s 2 s2 s2插入进来的时候,如果当前节点已经存在了回文子串,那么 s 2 s2 s2在当前节点的回文子串个数直接就可以加一,这样直接就判断出了和 s 1 s1 s1相同的子串个数。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 3e5+5;
const int N = 27;
const int mod = 19930726;

char s1[MAXN],s2[MAXN];

ll quick(ll a,ll b){
    ll res = 1;
    while(b){
        if(b&1) res = res*a%mod;
        a = a*a%mod;
        b >>= 1;
    }
    return res;
}
struct Palindromic_Tree {
	int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
	int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
	int cnt[MAXN] ; //表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
	int cnt2[MAXN];
	int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数。
	int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
	int S[MAXN] ;//存放添加的字符
	int last ;//指向上一个字符所在的节点,方便下一次add
	int n ;//字符数组指针
	int p ;//节点指针
	ll ans;
	int newnode ( int l ) {//新建节点
		for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ;
		cnt[p] = 0 ;
		cnt2[p] = 0;
		num[p] = 0 ;
		len[p] = l ;
		return p ++ ;
	}

	void init1 () {//初始化
		p = 0 ;
		newnode (  0 ) ;  //奇
		newnode ( -1 ) ;  //偶
		last = 0 ;
		n = 0 ;
		S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判
		fail[0] = 1 ;
	}
    void init2 () {//初始化
		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 add2 ( 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] ;
		cnt2[last] ++ ;
	}
	void count1 () {
		//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
        for(int i = p-1; i >= 0; i--){
            cnt[fail[i]] += cnt[i] ;
        }
	}
	void count2 () {
		//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
        for(int i = p-1; i >= 0; i--){
            cnt2[fail[i]] += cnt2[i] ;
        }
	}
	void solve(){
	    ans = 0;
        for(int i = 2; i <= p-1; i++)
            ans = ans + (ll)cnt[i]*cnt2[i];
        printf("%lld\n",ans);
	}

} pat;

int n,q;
int x1[MAXN],x2[MAXN];

int main(){
    int t,ca = 1;
    cin>>t;
    while(t--){
        //cout<<"s";
        scanf("%s%s",s1,s2);
        printf("Case #%d: ",ca++);
        pat.init1();
        int len = strlen(s1);
        int len2 = strlen(s2);
        for(int i = 0; i < len; i++){
            pat.add(s1[i]);
        }
        pat.count1();
        pat.init2();
        for(int i = 0; i < len2; i++){
            pat.add2(s2[i]);
        }
        pat.count2();
        pat.solve();
    }


    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值