-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZ-KMP.cpp
More file actions
53 lines (53 loc) · 827 Bytes
/
Copy pathZ-KMP.cpp
File metadata and controls
53 lines (53 loc) · 827 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 20000007
int n,m,z[N],p[N];
char a[N],b[N];
void init()
{
scanf("%s",a+1);
scanf("%s",b+1);
n=strlen(b+1);
m=strlen(a+1);
int maxl=0,maxr=0;
z[1]=n;
for(int i=2;i<=n;i++)
{
z[i]=min(max(maxr-i+1,0ll),z[i-maxl+1]);
while(b[i+z[i]]==b[z[i]+1]&&i+z[i]<=n)z[i]++;
if(i+z[i]-1>maxr)
maxr=i+z[i]-1,maxl=i;
}
}
void solve()
{
int maxl=0ll,maxr=0ll;
for(int i=1;i<=m;i++)
{
p[i]=min(max(maxr-i+1,0ll),z[i-maxl+1]);
while(a[i+p[i]]==b[p[i]+1]&&i+p[i]<=m)p[i]++;
if(i+p[i]-1>maxr)
maxr=i+p[i]-1,maxl=i;
// cout<<p[i]<<' ';
}
}
void print()
{
int an1=0,an2=0;
for(int i=1;i<=n;i++)
{
an1^=(z[i]+1)*i;
}
for(int i=1;i<=m;i++)
{
an2^=(p[i]+1)*i;
}
cout<<an1<<endl<<an2;
}
signed main()
{
init();
solve();
print();
}