-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteAgainAndAgain.cpp
More file actions
88 lines (76 loc) · 1.65 KB
/
Copy pathDeleteAgainAndAgain.cpp
File metadata and controls
88 lines (76 loc) · 1.65 KB
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
#include <bits/stdc++.h>
using namespace std;
const int N =1e6+10;
int stack1[N];
int stack2[N];
int size1;
vector<int> nextArray(const string&t) {
int m =t.length();
vector<int> next(m+1,0);
if (m==1) {
next[0]=-1;
return next;
}
next[0]=-1;
next[1]=0;
int i=2,ct=0;
while (i<m) {
if (t[i-1]==t[ct]) {
next[i++]=++ct;
}else if (ct>0) {
ct=next[ct];
}else {
next[i++]=0;
}
}
return next;
}
void kmp(const string&s,const string&t) {
string taget=s;
int len1=s.length();
int len2=t.length();
bool check=true;
int x=0,y=0;
vector<int> next =nextArray(t);
while (x<len1) {
if (t[y]==taget[x]) {
stack1[size1]=x;
stack2[size1]=y;
size1++;
x++;
y++;
}else if (y==0) {
stack1[size1]=x;
stack2[size1]=-1;
size1++;
x++;
}else {
y=next[y];
}
if (y==len2) {
size1 -= len2;
y= size1>0 ? (stack2[size1-1]+1) : 0;
}
}
// if (y==len2) {
// string source1 =taget.substr(0,x-y);
// string source2 =taget.substr(x);
// taget=source1+source2;
// }else {
// check=false;
// }
}
void solve() {
string s,t;
cin>>s>>t;
kmp(s,t);
for (int i=0;i<size1;i++) {
cout<<s[stack1[i]];
}
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}