[hdu5421]Victor and String

本文介绍了维克多喜欢玩字符串,特别是回文字符串,并描述了一个涉及四种操作的字符串游戏:在字符串开始或结束处添加字符,查询不同回文子串数量,以及总计所有回文子串数量。题目给出的限制和解决方案涉及到双端回文自动机,通过维护两个pos和last指针以及节点计数来高效处理操作和查询。
摘要由CSDN通过智能技术生成

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

这题bestcoder上现场没人过,标个3000分吧

Victor loves to play with string. He thinks a string is charming as the string is a palindromic string.
Victor wants to play n n 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 5 5 cases.
In each case, the first line has one integer n n 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 1≤n≤100000 1n100000.

Output
For each query operation(operation 3 or 4), print the correct answer.

题意:
你有四种操作:
1.往字符串的开头加一个字符
2.往字符串的结尾加一个字符
3.询问这个字符串的子串中本质不同的回文串的数量
4.询问这个字符串的子串中回文串的总数(相同的也要计算)
刚开始字符串长度是0

题解:
双端回文自动机
由于回文自动机的插入是O(1)的,所以可以借此来拓展一下回文自动机的应用范围。使其能够支持前后同时插入字符。
需要两个pos指针和两个last指针,分别左边界和右边界,last指针表示左边插入之后的最后一个节点,和右边插入之后的最后一个节点。
每个节点记录一个ca[x]表示当前节点所表示的字符串的数量。
对于询问3,我们只需要输出节点个数-2即可。
对于询问4,我们需要在插入的时候统计所有节点的ca[x]总和
当插入当前字符串之后,总串是个回文串的时候,这次插入操作的last可以被赋值给在另一个方向插入的last:

if(pos[1]-pos[0]+1==len[last[op]])last[op^1]=last[
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值