-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathZ_Algorithm_prefixMatching.cpp
More file actions
54 lines (53 loc) · 1.01 KB
/
Z_Algorithm_prefixMatching.cpp
File metadata and controls
54 lines (53 loc) · 1.01 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
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=n-1;i>=0;i--)
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
using namespace std;
string s;
ll n,l,r,ans;
ll z[100005];
void solve(){
n=s.length();
rep(i,n) z[i]=0;
z[0]=n;
l=0;
r=0;
ans=0;
for(int i=1;i<n;i++){
if(i>r){
l=i;
r=i;
while(r<n && s[r-l]==s[r]) r++;
z[i]=r-l;
r--;
}
else{
int k=i-l;
if(z[k]<r-i+1) z[i]=z[k];
else{
l=i;
while(r<n && s[r-l]==s[r]) r++;
z[i]=r-l;
r--;
}
}
}
rep(i,n) ans+=z[i];
cout<<ans<<endl;
}
int main(){
io
int t;
cin>>t;
while(t--){
cin>>s;
solve();
}
}