-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZ_algo.cpp
More file actions
63 lines (56 loc) · 1.43 KB
/
Z_algo.cpp
File metadata and controls
63 lines (56 loc) · 1.43 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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define present(c,val) ((c).find(val) != (c).end())
#define cpresent(c,val) (find(all(c),val) != (c).end())
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, x, y) for(int i= (x); i<= (y); ++i)
#define FORR(i, x, y) for(int i= (y); i>= (x); --i)
#define imax numeric_limits<int>::max()
#define imin numeric_limits<int>::min()
#define lmax numeric_limits<ll>::max()
#define lmin numeric_limits<ll>::min()
#define dmax numeric_limits<double>::max()
#define dmin numeric_limits<double>::min()
#define fmax numeric_limits<float>::max()
#define fmin numeric_limits<float>::min()
#define sqr(x) (x)*(x)
int z[3000005];
void computeZ(string s,int *z,int n) {
int l=0,r=0;
for(int i=1;i<n;i++) {
if(i>r) {
l=r=i;
while(r<n && s[r]==s[r-l]) r++;
z[i]=r-l;
r--;
}
else {
if(z[i-l]<r-i+1) z[i]=z[i-l];
else {
l=i;
while(r<n && s[r]==s[r-l]) r++;
z[i]=r-l;
r--;
}
}
}
return;
}
int main() {
int n;string s,t;cin>>n>>s>>t;
s=s+'#'+t+t;
computeZ(s,z,3*n+1);
int mx=0,mxi=0;
for(int i=n+1;i<2*n+1;i++) {if(z[i]>mx) mx=z[i],mxi=i-n-1;}
cout<<mxi<<endl;
return 0;
}