-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlifos.cpp
90 lines (80 loc) · 1.72 KB
/
Glifos.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long int lld;
typedef long double llf;
typedef pair<lld, lld> pii;
int n, m;
string s, t;
const int MAX_ALF = 60;
int bucket_S[MAX_ALF]; // Fija
int bucket_T[MAX_ALF]; // Vamos a andar modificando
int errores = 0;
int charToIdx(char c) {
if (c >= 'a' && c <= 'z') return c - 'a';
return c - 'A' + ('z' - 'a' + 1);
}
void update(char c, int v) {
// 1 -> mete, -1 -> saca
int idx = charToIdx(c);
if (bucket_S[idx] == bucket_T[idx]) {
// Eran igual, ya no lo seran
errores++;
}
bucket_T[idx] += v;
if (bucket_S[idx] == bucket_T[idx]) {
// Eran diferentes, pero ahora son iguales
errores--;
}
}
int barrido() {
int i = 0, j = 0;
for (j = 0; j < n; ++j) {
update(t[j], 1);
}
j--;
int ans = 0;
while (j < m) {
//cout << i << " " << j << " " << errores << endl;
if (errores == 0) {
// Ya hay un match
ans++;
}
if (j < m-1) {
// deslizar la ventana
update(t[j+1], 1);
update(t[i], -1);
}
i++;
j++;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
cin >> s >> t;
for (char c : s) {
int idx = charToIdx(c);
if (bucket_S[idx] == 0) {
errores++;
}
bucket_S[idx]++;
}
cout << barrido() << "\n";
return 0;
}