Description
要求兹磁以下操作:
1. 往串首加字符
2. 往串末加字符
3. 询问有多少个回文串
4. 询问有多少个本质不同的回文串
Solution
如果要兹磁前插,向后插一样记录一个last就行了,注意如果当前last的长度等于整个串的长度,那么修改一下另外一个last即可。
Code
/************************************************
* Au: Hany01
* Date: Jun 23rd, 2018
* Prob: [HDU5421] Victor and String
* Email: hany01@foxmail.com
* Institute: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 200005;
int l, r, pre, suf, len[maxn], fa[maxn], dep[maxn], ch[maxn][26], tot, las;
LL ans;
char s[maxn];
inline void init() {
fa[1] = fa[0] = 1, Set(ch, 0), len[tot = 1] = -1, ans = 0, l = (r = 1e5) + 1, suf = pre = 0, Set(s, 0);
}
inline void extend(int i, int &las, int ty)
{
register int p = las, c = (s[i] = getchar()) - 97;
while (s[i] != s[i - len[p] * ty - ty]) p = fa[p];
if (!ch[p][c]) {
register int np = ++ tot, k = fa[p];
while (s[i] != s[i - len[k] * ty - ty]) k = fa[k];
len[np] = len[p] + 2, dep[np] = dep[fa[np] = ch[k][c]] + 1, ch[p][c] = np;
}
ans += dep[las = ch[p][c]];
if (len[las] == r - l + 1) pre = suf = las;
}
int main()
{
#ifdef hany01
File("hdu5421");
#endif
static int T, opt;
while (~scanf("%d", &T)) {
init();
while (T --) {
opt = read();
if (opt < 3) opt == 1 ? extend(--l, pre, -1) : extend(++ r, suf, 1);
else opt == 3 ? printf("%d\n", tot - 1) : printf("%lld\n", ans);
}
}
#ifdef hany01
cerr << clock() * 1. / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
//一夜雨声凉到梦,万荷叶上送秋来。
// -- 陈文述《夏日杂诗》