forked from hughbzhang/Competitive_Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
censortest.cpp
130 lines (122 loc) · 2.98 KB
/
censortest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <cstdio>
#include <time.h>
#include <string>
#include <iostream>
using namespace std;
class node{
public:
node(){
for(int x = 0;x<26;x++){
arr[x] = NULL;
}
ok = false;
}
bool ok;//whether a word ends here
node* arr[26];//one for each letter (children)
};
int N;
string input;
int s[101000];
int n[101000];
int p[101000];
int slength;
int record = 0;
int BEGIN = 0;
node* start;
int length;
void print(){
for(int x = 1;x<slength;x++){
cout << char('a'+s[x]);
}
cout << endl;
int a = n[0];
while(a<slength){
cout << char(s[a]+'a');
a = n[a];
}
cout << endl;
}
int main(){
ios::sync_with_stdio(0);
double init = clock();
start = new node;
freopen("censor.in","r",stdin);
freopen("censor.out","w",stdout);
cin >> input;
length = input.length()+1;
slength = length;
for(int x = 1;x<=input.length();x++){
n[x] = x+1;
p[x+1] = x;
s[x] = input.at(x-1)-'a';
}
n[length] = -1;
n[0] = 1;
p[1] = 0;
p[0] = 0;
s[0] = -1;
cin >> N;
for(int x = 0;x<N;x++){
cin >> input;
if(input.length()>record) record = input.length();
node* cur = start;
for(int a = 0;a<input.length();a++){
int ind = input.at(a)-'a';
if(cur->arr[ind]==NULL) cur->arr[ind] = new node;
cur = cur->arr[ind];
}
cur->ok = true;
}
bool flag = true;
while(flag){
if((clock()-init)/CLOCKS_PER_SEC>1.5) break;
//cout << "LOL\n";
//print();
flag = false;
int beg = BEGIN;
while(beg<slength){
bool found = false;
beg = n[beg];
if(start->arr[s[beg]]==NULL) continue;
node* cur = start->arr[s[beg]];
int cnt = 1;
int a = beg;
while(a<slength){
if(cur->ok){
//cout << cnt << endl;
//
length-=cnt;
int skip = n[a];
while(cnt>0){
//cout << char(s[a]+'a');
a = p[a];
cnt--;
}
if(n[a]==skip) break;
n[a] = skip;
BEGIN = a;
for(int y = 0;y<record;y++){
BEGIN = p[BEGIN];
if(BEGIN==0) break;
}
p[skip] = a;
found = true;
flag = true;
break;
}
a = n[a];
if((cur->arr[s[a]])==NULL) break;
cur = cur->arr[s[a]];
cnt++;
}
if(found) break;
}
}
// print();
int a = n[0];
while(a<slength){
cout << char(s[a]+'a');
a = n[a];
}
cout << endl;
}