题意:要求维护两端加点的字符串,以及查询本质回文串个数和所有回文串个数

题解: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;
}
/******************** ********************/

最新文章

  1. JavaScript中ActiveXObject对象
  2. 基于MVC4+EasyUI的Web开发框架形成之旅--基类控制器CRUD的操作
  3. Vimium 快捷键记录
  4. C#实现通过模板自动创建Word文档的方法
  5. Excel合并单元格数据
  6. centos中安装jdk方法
  7. zzuli oj 1165 实数的小数部分(指针专题)
  8. Raft详解-启动后运行期间代码
  9. linux下制作u盘启动盘
  10. Vue服务端渲染和Vue浏览器端渲染的性能对比
  11. Android热插拔事件处理详解
  12. 新手vue构建单页面应用实例
  13. springcloud-hystrix断路器对微服务的容错处理
  14. mysql状态分析之show global status(转)
  15. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
  16. linux系统编程之信号(八):三种时间结构及定时器setitimer()详解
  17. 过虑器应用之1-设置request编码
  18. Objective-C:NSValue类的常见用法
  19. spark执行命令 监控执行命令
  20. Nginx入门篇(七)之Nginx+keepalived高可用集群

热门文章

  1. 16.2-uC/OS-III同步 (事件标志组)
  2. 6、Flutter Error waiting for a debug connection: ProcessException: adb did not report f(转)
  3. 合作开发工具——freeze和pipreqs
  4. Linux常用总结
  5. 大数据Spark+Kafka实时数据分析案例
  6. WinSDK(菜单笔记)
  7. 浅谈react的初步试用
  8. [openjudge-搜索]湖的深度
  9. Kubernetes持久化存储1——示例
  10. &lt;转&gt;jmeter(十九)HTTP属性管理器