-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbf4hard.cpp
More file actions
64 lines (64 loc) · 1.31 KB
/
Copy pathbf4hard.cpp
File metadata and controls
64 lines (64 loc) · 1.31 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
64
// Ivan Carvalho
// Solution to https://dmoj.ca/problem/bf4hard
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int freq[2010];
int esperado[2010];
char W[1000010], S[1000010];
int m, n;
int correto;
void add(char c) {
freq[c]++;
if (freq[c] == esperado[c]) {
correto++;
} else if (freq[c] == esperado[c] + 1) {
correto--;
}
}
void remove(char c) {
freq[c]--;
if (freq[c] == esperado[c]) {
correto++;
} else if (freq[c] == esperado[c] - 1) {
correto--;
}
}
int checa(int comeco) {
for (int i = comeco, j = 0; j < m; i++, j++) {
if (W[j] != S[i]) {
return 0;
}
}
return 1;
}
int main() {
set<char> conjunto;
scanf("%s", S);
scanf("%s", W);
n = strlen(S);
m = strlen(W);
for (int i = 0; i < m; i++) {
conjunto.insert(W[i]);
esperado[W[i]]++;
}
correto = 26 - (int)conjunto.size();
for (int i = 0; i < m; i++) {
add(S[i]);
}
if (correto == 26 && checa(0)) {
printf("0\n");
return 0;
}
for (int i = m; i < n; i++) {
remove(S[i - m]);
add(S[i]);
if (correto == 26 && checa(i - m + 1)) {
printf("%d\n", i - m + 1);
return 0;
}
}
printf("-1\n");
return 0;
}