MCPcopy Create free account
hub / github.com/Jonathan-Uy/CSES-Solutions / main

Function main

String Algorithms/String Matching.cpp:20–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20int main(){
21 scanf("%s %s", S, T);
22 N = (int) strlen(S); M = (int) strlen(T);
23
24 if(M > N){
25 printf("0\n");
26 return 0;
27 }
28
29 for(int p = 0; p < numP; p++){
30 pw[p][0] = 1;
31 for(int i = 1; i < maxN; i++)
32 pw[p][i] = (pw[p][i-1] * prime[p]) % MOD;
33 }
34
35 for(int i = 0; i < M; i++){
36 for(int p = 0; p < numP; p++){
37 sh[p] = (sh[p] + (S[i] - 'a' + 1) * pw[p][M-i-1]) % MOD;
38 th[p] = (th[p] + (T[i] - 'a' + 1) * pw[p][M-i-1]) % MOD;
39 }
40 }
41
42 if(hashes_equal()) cnt++;
43 for(int i = M; i < N; i++){
44 for(int p = 0; p < numP; p++){
45 sh[p] = (sh[p] * prime[p] - (S[i-M] - 'a' + 1) * pw[p][M]) % MOD;
46 sh[p] = (sh[p] + (S[i] - 'a' + 1) + MOD) % MOD;
47 }
48 if(hashes_equal()) cnt++;
49 }
50 printf("%d\n", cnt);
51}

Callers

nothing calls this directly

Calls 1

hashes_equalFunction · 0.85

Tested by

no test coverage detected