题目:http://acm.hdu.edu.cn/showproblem.php?pid=5421

因为要在前面插字符,所以维护一个前缀链和后缀链,在同一棵回文树上搞,如果有某个最长回文后缀(或前缀)的长度为总长,那让前缀(或后缀)的last也赋为当前结点。

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define ll long long
#define maxn 200500
using namespace std;
int n,op;
char c;
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
while (isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
struct data{
ll sum;
int L,R,tot,fail[maxn],l[maxn],nxt[maxn][],s[maxn],last[],cnt[maxn];
int newnode(int len){
clr(nxt[tot],);
l[tot]=len; cnt[tot]=;
return tot++;
}
void init(){
tot=; sum=;
newnode(); newnode(-);
fail[]=;
last[]=last[]=;
clr(s,-);
L=n; R=n-;
}
int getfail(int op,int v){
if (op) while (s[R-l[v]-]!=s[R]) v=fail[v];
else while (s[L+l[v]+]!=s[L]) v=fail[v];
return v;
}
void add(int op,int c){
if (op) s[++R]=c; else s[--L]=c;
int cur=getfail(op,last[op]);
if (!nxt[cur][c]){
int now=newnode(l[cur]+);
fail[now]=nxt[getfail(op,fail[cur])][c];
nxt[cur][c]=now;
cnt[now]=cnt[fail[now]]+;
} last[op]=nxt[cur][c];
if (l[last[op]]==R-L+) last[op^]=last[op];
sum+=cnt[last[op]];
}
}T;
int main(){
// freopen("in.txt","r",stdin);
while (~scanf("%d",&n)){
T.init();
rep(i,,n){
op=read();
if (op<=) {
c=getchar();
T.add(op-,c-'a');
}
else if(op==) printf("%d\n",T.tot-);
else printf("%lld\n",T.sum);
}
}
return ;
}

HDU-5421Victor and String的更多相关文章

  1. HDU 4054 Number String

    HDU 4054 Number String 思路: 状态:dp[i][j]表示以j结尾i的排列 状态转移: 如果s[i - 1]是' I ',那么dp[i][j] = dp[i-1][j-1] + ...

  2. hdu 5311 Hidden String

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Description Today is the 1st anniv ...

  3. hdu 5311 Hidden String (BestCoder 1st Anniversary ($))(深搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Time Limit: 2000/1000 MS (Java/Others)  ...

  4. HDU 5311 Hidden String (暴力)

    题意:今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为n的字符串s. 他想要知道能否找到s的三个互不相交的子串s[l1..r1], s[l2..r2], s[l3..r3]满足下 ...

  5. HDU 4668 Finding string (解析字符串 + KMP)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个压缩后的串,以及一个模式串,问模式串 ...

  6. HDU 5694 BD String 迭代

    BD String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5694 Description Problem Description 众所周知, ...

  7. HDU 4055 Number String dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4055 Number String Time Limit: 10000/5000 MS (Java/O ...

  8. hdu 4055 Number String

    Number String http://acm.hdu.edu.cn/showproblem.php?pid=4055 Time Limit: 10000/5000 MS (Java/Others) ...

  9. HDU 5707 Combine String(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5707 题意: 给你三个字符串 S1, S2, S3, 让你判断 S3 是否恰好由字符串 S1 和 S2 ...

  10. HDU 4055 Number String:前缀和优化dp【增长趋势——处理重复选数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意: 给你一个由'I', 'D', '?'组成的字符串,长度为n,代表了一个1~n+1的排列中 ...

随机推荐

  1. iOS 内存泄漏排查以及处理

    使用Xcode7的Instruments检测解决iOS内存泄露   文/笨笨的糯糯(简书作者)原文链接:http://www.jianshu.com/p/0837331875f0作为一名iOS开发攻城 ...

  2. c#创建access数据库和数据表

      由于在程序中使用了ADOX,所以先要在解决方案中引用之,方法如下: 解决方案资源管理器(项目名称)-->(右键)添加引用-->COM--> Microsoft ADO Ext. ...

  3. lua中易混淆函数

    lua中易混淆的函数 ipairs和pairs: ipairs只能顺序遍历table,遇到key不是数字就会退出 pairs可以遍历table中所有元素 ----------------------- ...

  4. bzoj 4817: [Sdoi2017]树点涂色

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  5. HTML知识点记录

    1.input的type设置为file时,设置multiple属性可以同时选择多个文件.

  6. js 变量、作用域和内存问题

    基本类型和引用类型 5种基本类型:undefined.null.boolean.number.string 引用类型:由多个值构成的对象 属性 引用类型可以动态添加属性,而基本类型不可以 var p ...

  7. @Autowired内部实现原理

    @Autowiredprivate CustomerDao customerDao;        public void addCustomer() {        customerDao.add ...

  8. Spark源码剖析(八):stage划分原理与源码剖析

    引言 对于Spark开发人员来说,了解stage的划分算法可以让你知道自己编写的spark application被划分为几个job,每个job被划分为几个stage,每个stage包括了你的哪些代码 ...

  9. PHP使用google api生成二维码

    代码如下: <?php $urlToEncode="nickname:Feanmy,email:feanmy@qq.com"; generateQRfromGoogle($u ...

  10. perl多线程使用

    原文来自:博客园(华夏35度)http://www.cnblogs.com/zhangchaoyang 作者:Orisun <<=========================threa ...