-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ21461.cc
45 lines (41 loc) · 879 Bytes
/
SPOJ21461.cc
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
// SPOJ 21461: Field
// http://www.spoj.com/problems/NPC2014F/
//
// Solution: windowing
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
int main() {
int n; scanf("%d", &n);
char s[100010]; scanf("%s", s);
int k; scanf("%d", &k);
int x[0x100] = {0};
for (int i = 0; i < k; ++i) {
int a;
char c[10];
scanf("%d %s", &a, c);
x[c[0]] = a;
}
int z[0x100] = {0};
int cond = 0;
int i = 0, j = 0;
int opt = n+1;
while (i < n) {
while (cond < k && j < n) {
char c = s[j++];
if (++z[c] == x[c]) ++cond;
}
if (cond >= k) {
opt = min(opt, j - i);
}
char c = s[i++];
if (z[c]-- == x[c]) --cond;
}
if (opt > n) printf("Andy rapopo\n");
else printf("%d\n", opt);
}