给出一个初始串,三种操作:
1.在这个字符串后面加个字符串。
2.给出一个长度len,问当前串长度为len的所有子串和长度小于len的所有后缀中字典序最小的起始坐标。
3.删除当前字符串后面len个字符。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 400005
#define maxm 100005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head
struct node
{
int len, suf, id;
bool *del;
node *fa, *next[26];
}pool[maxn], *root, *last;
node *pos[maxn];
bool del[maxn];
char s[maxn];
int tot, T, ok, n, m, k;
node* newnode(int len, int id)
{
node *p = &pool[tot++];
p->len = len, p->suf = 0, p->fa = 0, p->id = id;
p->del = &del[tot], *(p->del) = 0;
memset(p->next, 0, sizeof p->next);
return p;
}
void init()
{
T = tot = 0;
root = newnode(0, 0);
last = root;
}
node* add(int c, int id)
{
node *p = last, *np = newnode(p->len + 1, id);
last = np;
for(; p && (!p->next[c] || *(p->next[c]->del)); p = p->fa) p->next[c] = np;
if(!p) np->fa = root;
else {
node *q = p->next[c];
if(p->len + 1 == q->len) np->fa = q;
else {
node *nq = newnode(p->len + 1, q->id);
*nq = *q;
nq->len = p->len + 1;
q->fa = np->fa = nq;
for(; p && p->next[c] == q && !*(p->next[c]->del); p = p->fa) p->next[c] = nq;
}
}
return np;
}
void dfs(node *p, int step)
{
if(ok) return;
if(step == k || p->suf == T) {
if(step == k) printf("%d\n", p->id - step + 1);
else printf("%d\n", n - step + 1);
ok = true;
return;
}
for(int i = 0; i < 26; i++) if(p->next[i] && !*(p->next[i]->del)) dfs(p->next[i], step + 1);
}
void op1()
{
scanf("%s", s);
for(int i = 0; s[i]; i++) pos[n++] = add(s[i] - 'a', n+1);
}
void op2()
{
T++, ok = false;
scanf("%d", &k);
for(node *p = pos[n-1]; p; p = p->fa) p->suf = T;
root->suf = 0;
dfs(root, 0);
}
void op3()
{
int len;
scanf("%d", &len);
for(int i = n-1; i >= n-len; i--) *(pos[i]->del) = true;
n -= len;
if(n) last = pos[n-1];
else last = root;
}
void read()
{
n = strlen(s);
for(int i = 0; i < n; i++) pos[i] = add(s[i] - 'a', i+1);
scanf("%d", &m);
}
void work()
{
int op;
while(m--) {
scanf("%d", &op);
if(op == 1) op1();
if(op == 2) op2();
if(op == 3) op3();
}
}
int main()
{
while(scanf("%s", s)!=EOF) {
init();
read();
work();
}
return 0;
}