Victor and String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Others)Total Submission(s): 615 Accepted Submission(s): 181
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
BestCoder Round #52 (div.1) ($)
这道题要求前后端插入, 那么由于发现实际上回文串的性质是一个回文串的最长回文后缀等于最长回文前缀, 那么也就是fail实际上都是一样的... 那么我们只需要多维护一下当前最长回文后缀后最长回文前缀两个last就可以了, 注意有可能在修改最长回文后缀的时候会影响最长回文前缀(当且仅当此时这整个串就是回文串, 那么最长回文后缀和最长回文前缀是相等的).
#include<bits/stdc++.h>
#define setall(a, x) memset(a, x, sizeof(a))
using namespace std;
const int maxn = 2e5 + 5;
int n;
struct Palindromic_Tree {
int cnt, L, R;
long long ans;
int s[maxn], fail[maxn], len[maxn], num[maxn], c[maxn][26], last[2];
inline void init(int n) {
L = n, R = n - 1;
ans = cnt = 0;
setall(fail, 0), setall(s, -1);
newnode(0), newnode(-1);
fail[0] = 1, last[0] = last[1] = 1;
}
inline int newnode(int p) {
len[cnt] = p, num[cnt] = 0;
for (int ch = 0; ch < 26; ++ ch) c[cnt][ch] = 0;
return cnt ++;
}
inline int getfail(int p, bool d) {
if (d) while (s[R - len[p] - 1] != s[R]) p = fail[p];
else while (s[L + len[p] + 1] != s[L]) p = fail[p];
return p;
}
inline void add(int ch, bool d) {
if (L > R) d = 1;
if (d) s[++ R] = ch;
else s[-- L] = ch;
int cur = getfail(last[d], d);
if (!c[cur][ch]) {
int nw = newnode(len[cur] + 2);
fail[nw] = c[getfail(fail[cur], d)][ch];
c[cur][ch] = nw;
num[nw] = num[fail[nw]] + 1;
}
last[d] = c[cur][ch];
if (len[last[d]] == R - L + 1) last[d ^ 1] = last[d];
ans += num[last[d]];
}
}Ptr;
int main() {
while (scanf("%d", &n) != EOF) {
int opt;
char ch;
Ptr.init(n);
for (int i = 1; i <= n; ++ i) {
scanf("%d", &opt);
if (opt <= 2) {
scanf(" %c", &ch);
Ptr.add(ch - 'a', opt - 1);
}
else if (opt == 3) printf("%d\n", Ptr.cnt - 2);
else printf("%lld\n", Ptr.ans);
}
}
return 0;
}
再附上论文里所说的不基于势能分析的quick写法.
#include<bits/stdc++.h>
#define setall(a, x) memset(a, x, sizeof(a))
using namespace std;
const int maxn = 2e5 + 5;
int n;
struct Palindromic_Tree {
int cnt, L, R;
long long ans;
int s[maxn], fail[maxn], len[maxn], num[maxn], c[maxn][26], quick[maxn][26], last[2];
inline void init(int n) {
L = n, R = n - 1;
ans = cnt = 0;
setall(s, -1);
newnode(0), newnode(-1);
for (int i = 0; i < 26; ++ i) quick[0][i] = 1;
fail[0] = 1, last[0] = last[1] = 1;
}
inline int newnode(int p) {
len[cnt] = p, num[cnt] = 0, fail[cnt] = 0;
for (int ch = 0; ch < 26; ++ ch) c[cnt][ch] = quick[cnt][ch] = 0;
return cnt ++;
}
inline int getfail(int p, bool d) {
if (d) {
if (s[R - len[p] - 1] == s[R]) return p;
else return quick[p][s[R]];
} else {
if (s[L + len[p] + 1] == s[L]) return p;
else return quick[p][s[L]];
}
return p;
}
inline void fixquick(int p, bool d) {
if (d) quick[p][s[R - len[fail[p]]]] = fail[p];
else quick[p][s[L + len[fail[p]]]] = fail[p];
}
inline void add(int ch, bool d) {
if (L > R) d = 1;
if (d) s[++ R] = ch;
else s[-- L] = ch;
int cur = getfail(last[d], d);
if (!c[cur][ch]) {
int nw = newnode(len[cur] + 2);
fail[nw] = c[getfail(fail[cur], d)][ch];
memcpy(quick[nw], quick[fail[nw]], sizeof(quick[fail[nw]]));
fixquick(nw, d);
num[nw] = num[fail[nw]] + 1;
c[cur][ch] = nw;
}
last[d] = c[cur][ch];
if (len[last[d]] == R - L + 1) last[d ^ 1] = last[d];
ans += num[last[d]];
}
}Ptr;
int main() {
while (scanf("%d", &n) != EOF) {
int opt;
char ch;
Ptr.init(n);
for (int i = 1; i <= n; ++ i) {
scanf("%d", &opt);
if (opt <= 2) {
scanf(" %c", &ch);
Ptr.add(ch - 'a', opt - 1);
}
else if (opt == 3) printf("%d\n", Ptr.cnt - 2);
else printf("%lld\n", Ptr.ans);
}
}
return 0;
}