假设没有操作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
 

/* ***********************************************
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 suffix int 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. HDU 5421 Victor and String (回文自动机)

    题目大意:让你维护一个字符串,支持在开头结尾插入字符,以及查询本质不同的回文串数量以及回文串总数量 开头结尾都维护一个$last$指针,如果插入新字符后,整个串是一个回文串,就把另一个$last$赋值 ...

  2. HDOJ/HDU 2163 Palindromes(判断回文串~)

    Problem Description Write a program to determine whether a word is a palindrome. A palindrome is a s ...

  3. hdu5421 Victor and String 回文树(前后插入)

    题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...

  4. HDU5421 Victor and String 和 APIO2014 回文串

    两道差不多的题,都是回文自动机right集合处理相关. Victor and String Victor loves to play with string. He thinks a string i ...

  5. YZOI Easy Round 2_回文串 string

    原文链接:http://laphets1.gotoip3.com/?id=18 Description 给出一个由小写字母组成的字符串,其中一些字母被染黑了,用?表示.已知原来的串不是 一个回文串,现 ...

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

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

  7. Harry and magic string HDU - 5157 记录不相交的回文串对数

    题意: 记录不相交的回文串对数 题解: 正着反着都来一遍回文树 用sum1[i] 表示到 i 位置,出现的回文串个数的前缀和 sun2[i]表示反着的个数 ans+=sum1[i-1]*sum2[i] ...

  8. 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长度 ...

  9. HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)

    Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...

随机推荐

  1. springboot的登录拦截机制

    转自:https://blog.csdn.net/qq_26555463/article/details/78296103 如果是一个后台的管理项目的,有些东西是不能直接就可以访问的,必须要登录才可以 ...

  2. 洛谷 P2129 L国的战斗续之多路出击(模拟)

    P2129 L国的战斗续之多路出击 题目背景 广而告之:背景见其他L国的战斗!!大家一起刷 题目描述 这一次,L国决定军队分成n组,分布在各地,若以L国为原点,可以看作在一个直角坐标系内.但是他们都受 ...

  3. itext 生成pdf文档 小结(自己备忘)

    1.引入maven <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf& ...

  4. 微信小程序左右滑动切换页面示例代码--转载

    微信小程序——左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X, ...

  5. js动态追加的元素如何触发事件

    一般通过js或者jQuery动态添加的元素标签,通过该元素标签.class.id触发事件,是无效的.如下所示: <body> <input type="text" ...

  6. netty 引用计数对象(reference counted objects)

    [Netty官方文档翻译]引用计数对象(reference counted objects) http://damacheng009.iteye.com/blog/2013657

  7. 复习HTML+CSS(7)

    n  HTML 注释 <--! 注释内容 --> 注意:注释内容不会显示,注释是为了将来维护方面. n  图片热点(图像地图) 图像热点:给一张图片加多个链接,默认情况下,一张图片只能加一 ...

  8. java编译器和java解释器、JVM

    作用: JVM:JVM有自己完善的硬件架构,如处理器.堆栈(Stack).寄存器等,还具有相应的指令系统(字节码就是一种指令格式).JVM屏蔽了与具体操作系统平台相关的信息,使得Java程序只需要生成 ...

  9. android指纹识别、拼图游戏、仿MIUI长截屏、bilibili最美创意等源码

    Android精选源码 一个动画效果的播放控件,播放,暂停,停止之间的动画 用 RxJava 实现 Android 指纹识别代码 Android仿滴滴打车(滴滴UI)源码 Android高仿哔哩哔哩动 ...

  10. 利用MediaSession发送信息到蓝牙音箱

    1.利用MediaSession发送信息到蓝牙音箱,如:播放音乐时接收的歌曲信息,但是每一首歌连续播放时,再次发送的重复信息会被丢弃.则利用MediaSession发现信息时,要保证信息的不重复性. ...