题意:要求维护两端加点的字符串,以及查询本质回文串个数和所有回文串个数
题解:pam,两端加点过程详见ioi2017国家集训队论文,维护一个最长回文前缀和最长回文后缀即可,fail不用两个,能前后共用一个.维护所有回文串个数,就是用增量法,每加一个点计算含这个点的回文串的个数(即当前回文后缀),即fail链到0或1的长度,维护一个dep表示fail链长度即可.
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-7;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=100000+10,inf=0x3f3f3f3f;
struct PAM{
int ch[N][26],fail[N],len[N],s[N],dep[N];
int last[2],l,r,p;
ll ans;
int newnode(int w)
{
for(int i=0;i<26;i++)ch[p][i] = 0;
len[p] = w;
dep[p] = 0;
return p++;
}
void init()
{
l = 100000+1,r = 100000;
p = ans = 0;
last[0]=last[1]=0;
newnode(0);
newnode(-1);
memset(s,-1,sizeof s);
fail[0] = 1;
}
int getfail(int x,int op)
{
if(op)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,int op)
{
if(op)s[++r] = c;
else s[--l] = c;
int cur = getfail(last[op],op);
if(!ch[cur][c]){
int now = newnode(len[cur]+2);
fail[now] = ch[getfail(fail[cur],op)][c];
dep[now] = dep[fail[now]] + 1;
ch[cur][c] = now;
}
last[op] = ch[cur][c];
// printf("%d %d %d %d\n",op,cur,last[op],fail[last[op]]);
if(len[last[op]] == r-l+1)last[op^1]=last[op];
ans+=dep[last[op]];
}
}pam;
char s[10];
int main()
{
int n;
while(~scanf("%d",&n))
{
pam.init();
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
if(x<=2)
{
scanf("%s",s);
pam.add(s[0]-'a',x-1);
}
else if(x==3)printf("%d\n",pam.p-2);
else printf("%lld\n",pam.ans);
}
}
return 0;
}
/********************
********************/