HDU5421-Victor and String
来源:互联网 发布:齐天大圣羽翼进阶数据 编辑:程序博客网 时间:2024/05/22 03:35
Victor and String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Others)Total Submission(s): 585 Accepted Submission(s): 164
Problem Description
Victor loves to play with string. He thinks a string is charming as the string is a palindromic string.
Victor wants to playn times. Each time he will do one of following four operations.
Operation 1 : add a charc to the beginning of the string.
Operation 2 : add a charc 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.
Victor wants to play
Operation 1 : add a char
Operation 2 : add a char
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 integern means the number of operations.
The first number of nextn 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 .
In each case, the first line has one integer
The first number of next
Output
For each query operation(operation 3 or 4), print the correct answer.
Sample Input
61 a1 b2 a2 c3481 a2 a2 a1 a31 b34
Sample Output
454511
Source
BestCoder Round #52 (div.1) ($)
Recommend
hujie
题目:有n种操作,开始有一个空串,有4种操作:
1 c 在字符串的首部添加字符c
2 c 在字符串的尾部添加字符c
3 询问字符中回文串种类的个数
4 询问字符串中回文串的个数
解题思路:last[0]表示首部的操作的位置,last[1]表示尾部的操作的位置,当一整个字符串是回文串时,last[0]=last[1]
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int maxn = 1e6 + 10;char s[maxn];int n, x;struct PalindromicTree{const static int maxn = 1e6 + 10;int next[maxn][26], last[2], sz, tot[2];int fail[maxn], len[maxn];LL cnt[maxn];char s[maxn];void Clear(){len[1] = -1; len[2] = 0;fail[2] = fail[1] = 1;last[0] = last[1] = (sz = 3) - 1;cnt[1] = cnt[2] = 0;tot[0] = (tot[1] = n - 1) + 1;memset(next[1], 0, sizeof(next[1]));memset(next[2], 0, sizeof(next[2]));memset(s, 0, sizeof s);}int Node(int length){memset(next[sz], 0, sizeof(next[sz]));len[sz] = length, cnt[sz] = 1;return sz++;}int getfail(int x, int k){while (s[tot[k]] != s[tot[k] + (k ? -1 : 1) * (len[x] + 1)]) x = fail[x];return x;}LL add(char pos, int k){int x = (s[tot[k] += k ? 1 : -1] = pos) - 'a', y = getfail(last[k], k);if (next[y][x]) return cnt[last[k] = next[y][x]];last[k] = next[y][x] = Node(len[y] + 2);fail[last[k]] = len[last[k]] == 1 ? 2 : next[getfail(fail[y], k)][x];cnt[last[k]] += cnt[fail[last[k]]];if (len[last[k]] == tot[1] - tot[0] + 1) last[k ^ 1] = last[k];return cnt[last[k]];}}solve;int main(){while (~scanf("%d", &n)){LL ans = 0;solve.Clear();for (int i = 1; i <= n; i++){scanf("%d", &x);if (x <= 2){scanf("%s", s);ans += solve.add(s[0], x - 1);}else printf("%lld\n", x == 3 ? 1LL * solve.sz - 3 : ans);}}return 0;}
阅读全文
0 0
- HDU5421-Victor and String
- [HDU5421]Victor and String-回文树
- Victor and String
- HDU 5421 Victor and String
- HDOJ 5421 Victor and String 回文串自动机
- HDU 5421 Victor and String(回文树)
- Victor and Machine
- 【hdu5419】Victor and Toys
- Victor and Machine
- hdu 5417 Victor and Machine
- hdu 5417 Victor and Machine
- HDU 5417 Victor and Machine
- HDU-5417Victor and Machine
- hdu 5417 Victor and Machine
- hdu 5419 Victor and Toys
- HDOJ 5417 Victor and Machine
- hdu 5417 Victor and Machine
- BestCoder#52 Victor and World
- 1003. Emergency (25)
- 【bfs && 如何想到bfs】2014 Benelux Algorithm B
- PAT-甲级-1001 A+B Format
- 浅谈return
- python dict和set
- HDU5421-Victor and String
- scrapy爬虫-爬取慕课网全部课程
- 049 极值及最大值,最小值
- windows系统使用小技巧
- 请定义一个交通工具(Vehicle)的类,其中有:
- Unity_FPS游戏_Boss模块流程01脚本
- Linux常用命令与快捷键
- 贪心算法解决活动安排问题
- SDUT 1591 交叉排序