Skip to content

Commit ad96319

Browse files
committed
vjudge, ADS, KBTU, Rabin-Karp and Knuth-Morris-Pratt Algorithms
1 parent 7946f1d commit ad96319

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <stdio.h>
2+
#include <iostream>
3+
#include <fstream>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <queue>
7+
#include <deque>
8+
#include <stack>
9+
#include <set>
10+
#include <unordered_set>
11+
#include <map>
12+
#include <unordered_map>
13+
#include <cmath>
14+
#include <cstring>
15+
#include <bits/stdc++.h>
16+
17+
using namespace std;
18+
19+
#define F first
20+
#define S second
21+
#define pb push_back
22+
#define pf push_front
23+
#define ppb pop_back
24+
#define ppf pop_front
25+
#define mp make_pair
26+
#define speed ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
27+
28+
typedef long long ll;
29+
typedef unsigned long long ull;
30+
typedef pair<int, int> pii;
31+
32+
const int inf = 1e9;
33+
const int N = 1e5+5;
34+
const int mod = 1e9 + 7;
35+
const int MAX = 1e6;
36+
const int shift = 4;
37+
const int p[] = {31, 53};
38+
39+
class Solution {
40+
int t, n, k;
41+
string text;
42+
set<pair<ull, ull>> unique;
43+
vector<ull> p_pow[2], hash[2];
44+
45+
void print() {
46+
cout << unique.size() << endl;
47+
}
48+
49+
void input() {
50+
speed
51+
// freopen("input.txt", "r", stdin);
52+
// freopen("output.txt", "w", stdout);
53+
cin >> t;
54+
}
55+
56+
void output() {
57+
}
58+
59+
void solution() {
60+
calc_pows(N);
61+
for (int j = 0; j < t; j++) {
62+
cin >> n >> k >> text;
63+
prepare();
64+
65+
for (int i = 0; i < n - k + 1; i++) {
66+
ull cur[] = {hash[0][i + k - 1], hash[1][i + k - 1]};
67+
if (i) {
68+
cur[0] -= hash[0][i - 1];
69+
cur[1] -= hash[1][i - 1];
70+
}
71+
cur[0] *= p_pow[0][n - i - 1];
72+
cur[1] *= p_pow[1][n - i - 1];
73+
unique.insert(mp(cur[0], cur[1]));
74+
}
75+
76+
print();
77+
unique.clear();
78+
}
79+
}
80+
81+
void calc_pows(int k) {
82+
// all pows of p
83+
p_pow[0].resize(k);
84+
p_pow[1].resize(k);
85+
86+
p_pow[0][0] = 1;
87+
p_pow[1][0] = 1;
88+
89+
for (int i = 1; i < k; i++) {
90+
p_pow[0][i] = p_pow[0][i - 1] * p[0];
91+
p_pow[1][i] = p_pow[1][i - 1] * p[1];
92+
}
93+
}
94+
95+
void prepare() {
96+
// all hashes of text
97+
hash[0].resize(n);
98+
hash[1].resize(n);
99+
for (int i = 0; i < n; i++) {
100+
hash[0][i] = (text[i] - 'a' + 1) * p_pow[0][i];
101+
hash[1][i] = (text[i] - 'a' + 1) * p_pow[1][i];
102+
if (i) {
103+
hash[0][i] += hash[0][i - 1];
104+
hash[1][i] += hash[1][i - 1];
105+
}
106+
}
107+
}
108+
109+
public:
110+
111+
Solution() {
112+
}
113+
114+
void solve() {
115+
input();
116+
solution();
117+
output();
118+
}
119+
};
120+
121+
int main() {
122+
Solution s = Solution();
123+
s.solve();
124+
return 0;
125+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Task
2+
3+
Ada the Ladybug has decided to do some "Spring Cleaning". As you might know, she keeps a TODO list. She is very sparing so she keeps all her activities as one string. You might get very confused while reading the string but she has a system - every activity has length exactly K characters. Sadly, as new activities were added to the list many duplicities appeared. Now it is time to find out how many unique activities are in her TODO list.
4+
5+
# Input
6+
7+
First line contains T, number of test-cases.
8+
9+
Each test-case begins with N, K, 1 ≤ K ≤ N ≤ 105, length of string and length of activites respectively.
10+
11+
Next line consists of string of length N, consisting of lowercase letters.
12+
13+
The sum of lengths of strings among all test-cases won't exceed 3 * 1e5
14+
15+
# Output
16+
17+
For each test-case, print the number of unique substrings of length K
18+
19+
# Examples
20+
21+
## Input
22+
23+
5
24+
3 2
25+
aaa
26+
5 1
27+
abcba
28+
4 2
29+
abac
30+
10 2
31+
abbaaaabba
32+
7 3
33+
dogodog
34+
35+
## Output
36+
37+
1
38+
3
39+
3
40+
4
41+
4

0 commit comments

Comments
 (0)