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. Task WaitAll的用法

    var tasklst = new List<Task>(); ; i < urls.Count; i++) { tasklst.Add(Task.Factory.StartNew& ...

  2. base、this、new、override、abstract、virtual、static

    前言 本文主要来讲解一下C#中,自己觉得掌握的不怎么样或者用的不多,不太熟悉的关键字,主要包括base.this.new.override.abstract.virtual以及针对static字段和s ...

  3. LAMP架构三

    PHP相关配置 1.查找php配置文件/usr/local/php/bin/php -i或者phpinfo() [root@bogon admin]# /usr/local/php/bin/php - ...

  4. Windows 7/8/8.1 误删EFI启动项,无法开机解决方式(U盘+原版镜像)

    今天手贱把Windows 7的启动项删了.由于是GPT分区,EFI引导的,又不像MBR那般easy解决 想想重装系统也麻烦,并且仅仅是删了个启动项而已.就不是必需去费那个时间 想了一下,Windows ...

  5. vivado编译出错 [Synth 8-729] [Synth 8-787]

    http://www.cnblogs.com/pejoicen   打开vivado工程后,发现右上角如下图所示: 重新编译这两个ip核后,对整个工程synthesis,工程报错 [Synth 8-7 ...

  6. JS高程3:函数表达式

    定义函数的方式有2种: 函数声明 函数表达式 函数声明是最常用的,函数声明的一个特征就是:在执行代码之前,就已经读取了函数声明. 这个特征还有一个专门的术语:函数声明提升. 递归函数 所谓递归函数,就 ...

  7. 去掉IntelliJ IDEA代码编辑区域的竖线

    (网络配图) 作为从事编程或者测试工作的人来说,尤其是有强迫症的,看着非常痛苦,我们来看看怎么去掉 在 Settings-> Editor-> General-> Appearanc ...

  8. js 阻止事件冒泡 支持所有主流浏览器

    function getEvent(){ if(window.event) {return window.event;} func=getEvent.caller; while(func!=null) ...

  9. 【Mac + Appium + Python3.6学习(四)】之常用的IOS自动化测试API总结

    环境: Appium:1.9.1 Appium-desktop:1.7.1 Xcode:10.0 IOS:iPhone5S(10.3.3) Mac:10.13.6 ①获取手机屏幕size:长.宽 &l ...

  10. linux中常用压缩与解压命令

    一. tar文件的解压 tar -xvf db.tar 二. zip文件的压缩与解压 压缩 zip -r dest_name.zip directory_to_compress 解压 unzip de ...