hdu 5421回文树头尾插入更新查询

传送门

题意:每次可以从已有字符串的头部或者尾部插入一个字符,查询本质不同的回文子串个数和总共的回文子串个数

题解:难点就在于插入操作。

如果插入当前字符后,整个字符串并不是个回文串,那么从头插或者从尾插对结果无影响。但是如果插入后整个字符串构成了回文串,那么从尾插就会影响到从头插的结果。

因此维护两个头尾指针,其他的操作基本不变。注意如果当前插入操作的 l e n [ l a s t ] len[last] len[last]的回文子串长度刚好等于整个字符串长度,那么就要更新另一段的 l a s t last last。 还有由于维护了双指针,所以数组要开两倍。、

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5+5;
const int N = 27;
const int mod = 19930726;
ull base = 131,fac[MAXN],ha[MAXN];
ull geth(int l,int r){
    if(l == 0) return ha[r];
    return ha[r]-ha[l-1]*fac[r-l+1];
}
char s1[MAXN];
bool check(int l,int r){
    int mid = l+r>>1;
    int len = r-l+1;
    if(len&1) return geth(l,mid) == geth(mid,r);
    else
        return geth(l,mid) == geth(mid+1,r);
}
struct Palindromic_Tree {
	int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
	int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
	int cnt[MAXN] ; //表示节点i表示的本质不同的串的个数(建树时求出的不是完全的,最后count()函数跑一遍以后才是正确的)
	int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数。
	int len[MAXN] ;//len[i]表示节点i表示的回文串的长度
	int S[MAXN] ;//存放添加的字符
	int last[2] ;//指向上一个字符所在的节点,方便下一次add
	int n ;//字符数组指针
	int p ;//节点指针
    int L,R;
    ll ans;
	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 (int n) {//初始化
		p = 0 ;
		ans = 0;
		newnode (  0 ) ;
		newnode ( -1 ) ;
		last[0] = last[1] = 1;
		L = n;
		R = n-1;
		memset(S,-1,sizeof(S));//开头放一个字符集中没有的字符,减少特判
		fail[0] = 1 ;
	}
	int get_fail (int x ,bool dis) {//和KMP一样,失配后找一个尽量最长的
		if(dis) while(S[R-len[x]-1] != S[R]) x = fail[x];
		else while (S[L+len[x]+1] != S[L] ) x = fail[x] ;
		return x ;
	}
	void add ( int c, bool dis ) {
		c -= 'a' ;
		if(dis) S[++R] = c;
		else S[--L] = c ;
		int cur = get_fail ( last[dis], dis) ;//通过上一个回文串找这个回文串的匹配位置
		if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
			int now = newnode ( len[cur] + 2 ) ;//新建节点
			fail[now] = next[get_fail(fail[cur],dis)][c] ;//和AC自动机一样建立fail指针,以便失配后跳转
			next[cur][c] = now ;
			num[now] = num[fail[now]] + 1 ;
		}
		last[dis] = next[cur][c] ;
		if(len[last[dis]] == R-L+1)  //如果当前last刚好是整个字符串,就更新另外一端的。
            last[dis^1] = last[dis];
        ans += num[last[dis]];
		cnt[last[dis]]++ ;
	}
} pat;
int n;
int main(){
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n)){
        pat.init(n);
        int op;
        char ch[3];
        while(n--){
            scanf("%d",&op);
            if(op == 1){
                scanf("%s",ch);
                pat.add(ch[0],0);
            }
            else if(op == 2){
                scanf("%s",ch);
                pat.add(ch[0],1);
            }
            else if(op == 3){
                printf("%d\n",pat.p-2);
            }
            else{
                printf("%lld\n",pat.ans);
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值