题目: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. Hive数据倾斜解决方法总结

    数据倾斜是进行大数据计算时最经常遇到的问题之一.当我们在执行HiveQL或者运行MapReduce作业时候,如果遇到一直卡在map100%,reduce99%一般就是遇到了数据倾斜的问题.数据倾斜其实 ...

  2. js实现文字逐个出现动效

    效果 首先看下效果,这是在h5页面中常见的一中文字展现方式,那么是怎么实现的呢?其实很简单 思路 用一个定时器将预制的文字通过.substring(0, i)方法不断的赋给要显示的区域,i在定时器里面 ...

  3. Cookie的简单用法

    ASP.NET初学者使用cookie的时候会感觉很陌生,在学习的过程中掌握cookie对象的增删改查非常有必要,,下面是我学习的时候经常用到的这些方法 写入和读取Cookie都需要用户Respone对 ...

  4. 二分查找(折半查找)C++

    二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好,占用系统内存较少: 其缺点是要求待查表为有序表,且插入删除困难. 因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 首先,假设表 ...

  5. MySQL在字段中使用select子查询

    前几天看别人的代码中看到在字段中使用select子查询的方法,第一次见这种写法,然后研究了一下,记录下来 大概的形式是这样的: select a .*,(select b.another_field ...

  6. 修改Linux内核参数提高Nginx服务器并发性能

    当linux下Nginx达到并发数很高,TCP TIME_WAIT套接字数量经常达到两.三万,这样服务器很容易被拖死.事实上,我们可以简单的通过修改Linux内核参数,可以减少Nginx服务器 的TI ...

  7. Chrome headless 模式

    我们在通过Selenium运行自动化测试时,必须要启动浏览器,浏览器的启动与关闭必然会影响执行效率,而且还会干扰你做其它事情(本机运行的话). 那能不能把自动化测试的运行放在后台?当然可以! html ...

  8. 使用Word进行文档修订版本的比较

    项目经理在实际的工作过程中,比如要写文档方案,就需要对文档的修订版本进行管理和控制.在以前的工作中,笔者使用的是UltraEdit这个软件工具中的Ultra Compare这个子工具来进行的文档版本的 ...

  9. C# 多线程传递多个参数

    http://www.cnblogs.com/lvdongjie/p/5416883.html 3. 方式三:采用lambda表达式 对于lambda表达式不熟悉的可以查看微软MSDN上的说明文档.此 ...

  10. linux保持管道中颜色显示

    在linux工作中,不同类型的文件以不同的颜色显示,如文件夹显示蓝色,压缩文件显示橘黄色,可执行文件显示为绿色,链接失效文件高亮显示等等: 有时候根据颜色可以快速鉴别,如我有时为了保持目录的完整性,会 ...