#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX = 400020;
int circle, n;
bool data[MAX<<1];
void input(){
scanf("%d%d", &circle, &n);
n <<= 1;
int i;
for(i = 0; i<n; i++){
char t[2];
scanf("%*d%s", t);
data[i+n] = data[i] = t[0] == 'W';
}
}
int fail[MAX];
void calcFail(){
fail[0] = -1;
int i;
for(i = 1; i<n; i++){
int last = i-1;
fail[i] = -1;
while(last >= 0){
if(data[i] == data[fail[last]+1]) {
fail[i] = fail[last]+1;
break;
} else last = fail[last];
}
}
}
int res = 0;
void check(int pos){
int t = min(pos, n-pos);
if(res == 0 || res > t) res = t;
}
void match(){
int f = 0, s;
for(s = 0; s<2*n; s++){
while(f > 0 && data[f] == data[s])
f = fail[f-1]+1;
if(data[f] != data[s]) f++;
if(f == n){
check(s-(n-1));
f = fail[n-1]+1;
}
}
}
int main(){
input();
calcFail();
match();
printf("%lld\n", res == 0 ? -1 : (long long)res * circle);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3432 KB |
Output is correct |
2 |
Correct |
0 ms |
3432 KB |
Output is correct |
3 |
Correct |
0 ms |
3432 KB |
Output is correct |
4 |
Correct |
0 ms |
3432 KB |
Output is correct |
5 |
Correct |
0 ms |
3432 KB |
Output is correct |
6 |
Correct |
0 ms |
3432 KB |
Output is correct |
7 |
Correct |
0 ms |
3432 KB |
Output is correct |
8 |
Correct |
0 ms |
3432 KB |
Output is correct |
9 |
Correct |
0 ms |
3432 KB |
Output is correct |
10 |
Correct |
0 ms |
3432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3432 KB |
Output is correct |
2 |
Correct |
0 ms |
3432 KB |
Output is correct |
3 |
Correct |
0 ms |
3432 KB |
Output is correct |
4 |
Correct |
0 ms |
3432 KB |
Output is correct |
5 |
Correct |
0 ms |
3432 KB |
Output is correct |
6 |
Correct |
0 ms |
3432 KB |
Output is correct |
7 |
Correct |
0 ms |
3432 KB |
Output is correct |
8 |
Correct |
0 ms |
3432 KB |
Output is correct |
9 |
Correct |
0 ms |
3432 KB |
Output is correct |
10 |
Correct |
0 ms |
3432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3432 KB |
Output is correct |
2 |
Correct |
0 ms |
3432 KB |
Output is correct |
3 |
Correct |
0 ms |
3432 KB |
Output is correct |
4 |
Correct |
0 ms |
3432 KB |
Output is correct |
5 |
Correct |
0 ms |
3432 KB |
Output is correct |
6 |
Correct |
0 ms |
3432 KB |
Output is correct |
7 |
Correct |
0 ms |
3432 KB |
Output is correct |
8 |
Correct |
0 ms |
3432 KB |
Output is correct |
9 |
Correct |
0 ms |
3432 KB |
Output is correct |
10 |
Correct |
0 ms |
3432 KB |
Output is correct |
11 |
Correct |
0 ms |
3432 KB |
Output is correct |
12 |
Correct |
0 ms |
3432 KB |
Output is correct |
13 |
Correct |
0 ms |
3432 KB |
Output is correct |
14 |
Correct |
0 ms |
3432 KB |
Output is correct |
15 |
Correct |
0 ms |
3432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3432 KB |
Output is correct |
2 |
Correct |
0 ms |
3432 KB |
Output is correct |
3 |
Correct |
0 ms |
3432 KB |
Output is correct |
4 |
Correct |
0 ms |
3432 KB |
Output is correct |
5 |
Correct |
0 ms |
3432 KB |
Output is correct |
6 |
Correct |
0 ms |
3432 KB |
Output is correct |
7 |
Correct |
0 ms |
3432 KB |
Output is correct |
8 |
Correct |
0 ms |
3432 KB |
Output is correct |
9 |
Correct |
0 ms |
3432 KB |
Output is correct |
10 |
Correct |
0 ms |
3432 KB |
Output is correct |
11 |
Correct |
0 ms |
3432 KB |
Output is correct |
12 |
Correct |
0 ms |
3432 KB |
Output is correct |
13 |
Correct |
0 ms |
3432 KB |
Output is correct |
14 |
Correct |
0 ms |
3432 KB |
Output is correct |
15 |
Correct |
0 ms |
3432 KB |
Output is correct |
16 |
Correct |
0 ms |
3432 KB |
Output is correct |
17 |
Correct |
0 ms |
3432 KB |
Output is correct |
18 |
Correct |
0 ms |
3432 KB |
Output is correct |
19 |
Correct |
0 ms |
3432 KB |
Output is correct |
20 |
Correct |
0 ms |
3432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
3432 KB |
Output is correct |
2 |
Correct |
48 ms |
3432 KB |
Output is correct |
3 |
Correct |
40 ms |
3432 KB |
Output is correct |
4 |
Correct |
20 ms |
3432 KB |
Output is correct |
5 |
Correct |
52 ms |
3432 KB |
Output is correct |
6 |
Correct |
76 ms |
3432 KB |
Output is correct |
7 |
Correct |
68 ms |
3432 KB |
Output is correct |
8 |
Correct |
80 ms |
3432 KB |
Output is correct |
9 |
Correct |
64 ms |
3432 KB |
Output is correct |
10 |
Correct |
80 ms |
3432 KB |
Output is correct |
11 |
Correct |
68 ms |
3432 KB |
Output is correct |
12 |
Correct |
100 ms |
3432 KB |
Output is correct |
13 |
Correct |
84 ms |
3432 KB |
Output is correct |
14 |
Correct |
96 ms |
3432 KB |
Output is correct |
15 |
Correct |
88 ms |
3432 KB |
Output is correct |
16 |
Correct |
84 ms |
3432 KB |
Output is correct |
17 |
Correct |
84 ms |
3432 KB |
Output is correct |
18 |
Correct |
72 ms |
3432 KB |
Output is correct |
19 |
Correct |
100 ms |
3432 KB |
Output is correct |
20 |
Correct |
84 ms |
3432 KB |
Output is correct |
21 |
Correct |
80 ms |
3432 KB |
Output is correct |
22 |
Correct |
84 ms |
3432 KB |
Output is correct |
23 |
Correct |
80 ms |
3432 KB |
Output is correct |
24 |
Correct |
88 ms |
3432 KB |
Output is correct |
25 |
Correct |
84 ms |
3432 KB |
Output is correct |
26 |
Correct |
88 ms |
3432 KB |
Output is correct |
27 |
Correct |
96 ms |
3432 KB |
Output is correct |
28 |
Correct |
92 ms |
3432 KB |
Output is correct |
29 |
Correct |
72 ms |
3432 KB |
Output is correct |
30 |
Correct |
80 ms |
3432 KB |
Output is correct |