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 1≤n≤100000.
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[