Victor and String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Others)

Total Submission(s): 163    Accepted Submission(s): 78

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
这道题目和简单的回文树应用不一样了,它是在两头加字符,不是从左到右只加一边。那么
我们怎么解决这样的问题呢?
首先s[]数组应该开到长度的两倍,然后以中间作为起始点,分别向两边扩展。
然后last应该有两个指针,一个指向左边,一个指向右边
另外也应该有两个指针表示两边数组的长度,距离中间的长度
在加入新节点的时候,从左边插,和从右边插是一样的,只是有一点
在插入一边的时候可能会应该回影响另一边的last指针。具体见代码
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
typedef long long int LL;
const int MAX=1e5+5;
char str[MAX];
int n;
struct Tree
{
int next[2*MAX][26];
int fail[2*MAX];
int num[2*MAX];
int len[2*MAX];
int s[2*MAX];
int last[2];
int tot[2];
LL p;
int new_node(int x)
{
memset(next[p],0,sizeof(next[p]));
num[p]=0;
len[p]=x;
return p++;
}
void init()
{
p=0;
new_node(0);
new_node(-1);
last[0]=0;
last[1]=0;
tot[0]=MAX-5;
tot[1]=MAX-6;
fail[0]=1;
}
int get_fail(int x,int k)
{
s[tot[0]-1]=-1;s[tot[1]+1]=-1;
while(s[tot[k]]!=s[tot[k]+(k?-1:1)*(len[x]+1)])
x=fail[x];
return x;
}
int add(int x,int k)
{
x-='a';
s[tot[k]+=(k?1:-1)]=x;
int cur=get_fail(last[k],k);
if(!(last[k]=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur],k)][x];
next[cur][x]=now;
num[now]=num[fail[now]]+1;
last[k]=now;
if(len[last[k]]==tot[1]-tot[0]+1) last[k^1]=last[k];
}
return num[last[k]];
}
}tree;
int main()
{
int x;char y[10];
while(scanf("%d",&n)!=EOF)
{
tree.init();
LL ans=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x==1)
{
scanf("%s",y);
ans+=tree.add(y[0],0);
}
else if(x==2)
{
scanf("%s",y);
ans+=tree.add(y[0],1);
} else if(x==3)
printf("%d\n",tree.p-2);
else
printf("%lld\n",ans);
}
}
return 0;
}


 

HDU 5421 Victor and String(回文树)的更多相关文章

  1. HDU 5421 Victor and String (回文自动机)

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

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

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

  3. HDOJ 5421 Victor and String 回文串自己主动机

    假设没有操作1,就是裸的回文串自己主动机...... 能够从头部插入字符的回文串自己主动机,维护两个last点就好了..... 当整个串都是回文串的时候把两个last统一一下 Victor and S ...

  4. HDU 5157 Harry and magic string(回文树)

    Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. HDU 6599 I Love Palindrome String (回文树+hash)

    题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...

  6. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  7. 2019 徐州网络赛 G Colorful String 回文树

    题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...

  8. HDU 5421 Victor and String

    Victor and String Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on HDU. Orig ...

  9. HDU - 5785:Interesting (回文树,求相邻双回文的乘积)

    Alice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three ...

随机推荐

  1. Ajax高级应用---Comet

    非常适合处理体育比赛的分数和股票报价 1.HTTP流 将输出缓存中的内容一次性全部发送到客户端的功能是实现HTTP流的关键所在.

  2. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  3. HDOJ 5418 Victor and World 状压DP

    水状压DP Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java ...

  4. Editplus 文件中批量搜索字符串的技巧

    常规情况下,我们利用Crtl+F可以在文档中查找字符串,进行替换等操作. 但要有的时候,我们要在大量文件中做这种查找操作,显然,一个个的打开文档是不现实的. 比如: 最近,谷歌被墙的很厉害,导致很多w ...

  5. Springboot读取配置文件的两种方法

    第一种: application.yml配置中的参数: zip: Hello Springboot 方法读取: @RestController public class ControllerTest ...

  6. Django--ORM基础

    ORM(映射关系) 映射关系: 表名 <-------> 类名 字段 <-------> 属性 表记录 <------->类实例对象 创建表(建立模型) 在Djan ...

  7. python之中国大学爬虫

    #!/usr/bin/env python3 #-*- coding:utf-8 -*- ############################ #File Name: zuihaodaxuepai ...

  8. TCP/IP详解读书笔记:链路层

    在TCP/IP协议族中,链路层主要有三个目的: 1)为IP模块发送和接受IP数据报: 2)为ARP模块发送ARP请求和接受ARP应答: 3)为RARP模块发送RARP请求和接受RARP应答: 以太网和 ...

  9. IPC之共享内存

    man 7 shm_overview shm_overview - Overview of POSIX shared memory. 同样,SystemV实现的共享内存是旧的机制,但应用广泛:Posi ...

  10. 【活动】上线了|带你直击react年度盛会

    明后两天,ReactEurope 2016大会在巴黎举行,本次大会演讲主题有: React Native(动画及运行性能优化) Flux-like 数据架构(GraphQL 最佳实践与展望.Redux ...