假设没有操作1,就是裸的回文串自己主动机......

能够从头部插入字符的回文串自己主动机,维护两个last点就好了.....

当整个串都是回文串的时候把两个last统一一下

Victor and String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Others)
Total Submission(s): 30    Accepted Submission(s): 9

Problem Description
Victor loves to play with string. He thinks a string is charming as the string is a palindromic string.

Victor wants to play n times. Each time he will do one of following four operations.

Operation 1 : add a char c to the beginning of the string.

Operation 2 : add a char c to the end of the string.

Operation 3 : ask the number of different charming substrings.

Operation 4 : ask the number of charming substrings, the same substrings which starts in different location has to be counted.

At the beginning, Victor has an empty string.

Input
The input contains several test cases, at most 5 cases.

In each case, the first line has one integer n means the number of operations.

The first number of next n line is the integer op, meaning the type of operation. If op=1 or 2, there will be a lowercase English letters followed.

1≤n≤100000.

Output
For each query operation(operation 3 or 4), print the correct answer.
Sample Input
6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4
Sample Output
4 5 4 5 11
Source
BestCoder Round #52 (div.1) ($)
/* ***********************************************
Author        :CKboss
Created Time  :2015年08月24日 星期一 10时32分04秒
File Name     :HDOJ5421.cpp
************************************************ */#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>using namespace std;typedef long long int LL;const int maxn=200100;
const int C=30;int L,R;
int nxt[maxn][C];
int fail[maxn];
LL cnt[maxn];
LL num[maxn];
int len[maxn];
int s[maxn];
int last[2],p,n;
LL tot;int newnode(int x)
{for(int i=0;i<C;i++) nxt[p][i]=0;num[p]=0; len[p]=x;return p++;
}void init()
{p=0;newnode(0); newnode(-1);memset(s,-1,sizeof(s));L=maxn/2; R=maxn/2-1;last[0]=last[1]=0;fail[0]=1;tot=0;
}/// d=0 add preffix d=1 add suffixint getfail(int d,int x)
{if(d==0) while(s[L+len[x]+1]!=s[L]) x=fail[x];else if(d==1) while(s[R-len[x]-1]!=s[R]) x=fail[x];return x;
}void add(int d,int c)
{c-='a';if(d==0) s[--L]=c;else s[++R]=c;int cur=getfail(d,last[d]);if(!nxt[cur][c]){int now=newnode(len[cur]+2);fail[now]=nxt[getfail(d,fail[cur])][c];nxt[cur][c]=now;num[now]=num[fail[now]]+1;}last[d]=nxt[cur][c];if(len[last[d]]==R-L+1) last[d^1]=last[d];tot+=num[last[d]];
}int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int _;while(scanf("%d",&_)!=EOF){init();while(_--){int kind;char ch[5];scanf("%d",&kind);if(kind<=2){kind--;scanf("%s",ch);add(kind,ch[0]);}else if(kind==3) printf("%d\n",p-2);else if(kind==4) printf("%lld\n",tot);}}return 0;
}

HDOJ 5421 Victor and String 回文串自己主动机相关推荐

  1. c++ string 回文串_第33期:上海自来水来自海上,回文字符串验证!

    我准备了 1000 本电子书和计算机各领域高清思维导图 100 张,关注后回复[资源],即可获取!更可回复[内推]加入 BAT 内推群! 01.题目示例 见微知著,发现一组数据很有趣,分享给大家.le ...

  2. c++ string 回文串_C++刷题——2802: 判断字符串是否为回文

    Description 编写程序,判断输入的一个字符串是否为回文.若是则输出"Yes",否则输出"No".所谓回文是指順读和倒读都是一样的字符串. Input ...

  3. [Leedcode][JAVA][第125题][验证回文串][双指针][String]

    [问题描述][简单] 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写.说明:本题中,我们将空字符串定义为有效的回文串.示例 1:输入: "A man, a p ...

  4. 回文串判断(string类:反转reverse)

    2029-Palindromes _easy version Problem Description "回文串"是一个正读和反读都一样的字符串,比如"level" ...

  5. HDOJ 1282 回文数猜想(回文串类)

    Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其 ...

  6. 2015 UESTC Training for Search Algorithm String - M - Palindromic String【Manacher回文串】

    O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...

  7. 2018ICPC南京 Problem M. Mediocre String Problem(回文串,马拉车,扩展KMP)

    题意: 给你两个字符串 s , t s,t s,t,要求从 s s s中找到一个子串和 t t t的一个前缀拼起来,结果要是回文串.求多少种拼法. 思路: 借此题复习了一下字符串算法. 首先 s s ...

  8. 伍六七带你学算法 入门篇-最长回文串

    力扣解题,每日一题:409. 最长回文串 难度- 简单 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" ...

  9. usaco Calf Flac(O(n)时间求回文串长度)

    好高兴,为数不多的我没看题解的题·,我用的是O(n)时间求回文串长度的算法算法在我上一篇博客. 然后就是注意细节了. /* ID:jinbo wu TASK: calfflac LANG:C++ */ ...

最新文章

  1. Linux那些事儿之我是Sysfs(9)sysfs文件系统模型
  2. 【CyberSecurityLearning 11】OSI与TCP/IP协议簇以及Packet Tracer模拟器
  3. mybatis批量夺标添加_MyBatis+MySQL同时执行多条SQL,实现多表插入数据
  4. jsp当前页的变量值显示到文本框中hint_Word中常用的这9个打印技巧,你不一定全懂,非常实用...
  5. 利用计算机制作多媒体最后一步,福建省高中会考 多媒体技术应用 选择题专项练习十一(201206)(有答案)...
  6. 【转】TCP/IP协议--TCP的超时和重传
  7. Linux进程实践(2) --僵尸进程与文件共享
  8. python接口自动化测试一:http协议
  9. JavaScript提高:003:easy UI实现tab页面自适应问题
  10. 【servlet】servlet基础知识总结
  11. poi(easypoi)导出excel(xls,xlsx)后,文件打开错误或乱码的解决方法(亲测)
  12. Java 编程语言单词汇总
  13. Resnet残差网络学习
  14. python中的round()函数
  15. 申宝股票-大盘缩量调整
  16. 如何解读肠道菌群检测报告中维生素指标
  17. 【Cesium入门】四、相机系统
  18. python 背景音乐程序代码_python 喜马拉雅 音乐下载 演示代码
  19. html5 show 案例
  20. navigationBar 标题字体颜色设置

热门文章

  1. 用 MySQL 实现分布式锁,你听过吗?
  2. Java 命名规范(非常全)
  3. 3W 字的 Spring Boot 超详细总结
  4. 需要搭建一个高性能的文件系统?我推荐你试试它.....
  5. 基于Transformers入门自然语言处理!
  6. 两位院士同时受聘,山东大学再添强援
  7. 看图学NumPy:掌握n维数组基础知识点,看这一篇就够了
  8. 又一壮举!GPT-3首次完成剧本创作,AI解决创造性问题的能力正迅速提升
  9. 阿里工程师力荐的计算机网络和算法资料,限时下载!
  10. 学C++,能不能简单点?