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 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.
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 .
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
Source
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 long
const 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;
}