#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<set>
#include<ctype.h>
using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> Pi;
int c[800040];
int f[400020];
int main()
{
int r, n;
int i;
scanf("%d%d",&r,&n);
for(i=1;i<=2*n;i++){
char ch[2];
scanf("%*d%s",ch);
c[i] = ch[0] == 'B';
}
n = 2*n;
for(i=1;i<=n;i++)c[i+n] = c[i];
int m = 2*n;
f[0] = -1;
for(i=1;i<=n;i++){
int x;
for(x = f[i-1]+1;x;x = f[x-1]+1){
if(c[i] == c[x])break;
}
f[i] = x;
}
int j = 1;
for(i=1;i<=m;i++,j++){
while(j && c[i] == c[j])j = f[j-1] + 1;
if(j == n)break;
}
if(i-n == n+1)printf("-1");
else printf("%lld",(ll)r*(i-n));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5776 KB |
Output is correct |
2 |
Correct |
0 ms |
5776 KB |
Output is correct |
3 |
Correct |
0 ms |
5776 KB |
Output is correct |
4 |
Correct |
0 ms |
5776 KB |
Output is correct |
5 |
Correct |
0 ms |
5776 KB |
Output is correct |
6 |
Correct |
0 ms |
5776 KB |
Output is correct |
7 |
Correct |
0 ms |
5776 KB |
Output is correct |
8 |
Correct |
0 ms |
5776 KB |
Output is correct |
9 |
Correct |
0 ms |
5776 KB |
Output is correct |
10 |
Correct |
0 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5776 KB |
Output is correct |
2 |
Correct |
0 ms |
5776 KB |
Output is correct |
3 |
Correct |
0 ms |
5776 KB |
Output is correct |
4 |
Correct |
0 ms |
5776 KB |
Output is correct |
5 |
Correct |
0 ms |
5776 KB |
Output is correct |
6 |
Correct |
0 ms |
5776 KB |
Output is correct |
7 |
Correct |
0 ms |
5776 KB |
Output is correct |
8 |
Correct |
0 ms |
5776 KB |
Output is correct |
9 |
Correct |
0 ms |
5776 KB |
Output is correct |
10 |
Correct |
0 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5776 KB |
Output is correct |
2 |
Correct |
0 ms |
5776 KB |
Output is correct |
3 |
Correct |
0 ms |
5776 KB |
Output is correct |
4 |
Correct |
0 ms |
5776 KB |
Output is correct |
5 |
Correct |
0 ms |
5776 KB |
Output is correct |
6 |
Correct |
0 ms |
5776 KB |
Output is correct |
7 |
Correct |
0 ms |
5776 KB |
Output is correct |
8 |
Correct |
0 ms |
5776 KB |
Output is correct |
9 |
Correct |
0 ms |
5776 KB |
Output is correct |
10 |
Correct |
0 ms |
5776 KB |
Output is correct |
11 |
Correct |
0 ms |
5776 KB |
Output is correct |
12 |
Correct |
0 ms |
5776 KB |
Output is correct |
13 |
Correct |
0 ms |
5776 KB |
Output is correct |
14 |
Correct |
0 ms |
5776 KB |
Output is correct |
15 |
Correct |
0 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5776 KB |
Output is correct |
2 |
Correct |
0 ms |
5776 KB |
Output is correct |
3 |
Correct |
0 ms |
5776 KB |
Output is correct |
4 |
Correct |
0 ms |
5776 KB |
Output is correct |
5 |
Correct |
0 ms |
5776 KB |
Output is correct |
6 |
Correct |
0 ms |
5776 KB |
Output is correct |
7 |
Correct |
0 ms |
5776 KB |
Output is correct |
8 |
Correct |
0 ms |
5776 KB |
Output is correct |
9 |
Correct |
0 ms |
5776 KB |
Output is correct |
10 |
Correct |
0 ms |
5776 KB |
Output is correct |
11 |
Correct |
0 ms |
5776 KB |
Output is correct |
12 |
Correct |
0 ms |
5776 KB |
Output is correct |
13 |
Correct |
0 ms |
5776 KB |
Output is correct |
14 |
Correct |
0 ms |
5776 KB |
Output is correct |
15 |
Correct |
0 ms |
5776 KB |
Output is correct |
16 |
Correct |
0 ms |
5776 KB |
Output is correct |
17 |
Correct |
0 ms |
5776 KB |
Output is correct |
18 |
Correct |
0 ms |
5776 KB |
Output is correct |
19 |
Correct |
0 ms |
5776 KB |
Output is correct |
20 |
Correct |
0 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
5776 KB |
Output is correct |
2 |
Correct |
48 ms |
5776 KB |
Output is correct |
3 |
Correct |
44 ms |
5776 KB |
Output is correct |
4 |
Correct |
20 ms |
5776 KB |
Output is correct |
5 |
Correct |
40 ms |
5776 KB |
Output is correct |
6 |
Correct |
84 ms |
5776 KB |
Output is correct |
7 |
Correct |
60 ms |
5776 KB |
Output is correct |
8 |
Correct |
80 ms |
5776 KB |
Output is correct |
9 |
Correct |
60 ms |
5776 KB |
Output is correct |
10 |
Correct |
76 ms |
5776 KB |
Output is correct |
11 |
Correct |
56 ms |
5776 KB |
Output is correct |
12 |
Correct |
84 ms |
5776 KB |
Output is correct |
13 |
Correct |
76 ms |
5776 KB |
Output is correct |
14 |
Correct |
76 ms |
5776 KB |
Output is correct |
15 |
Correct |
88 ms |
5776 KB |
Output is correct |
16 |
Correct |
80 ms |
5776 KB |
Output is correct |
17 |
Correct |
88 ms |
5776 KB |
Output is correct |
18 |
Correct |
80 ms |
5776 KB |
Output is correct |
19 |
Correct |
84 ms |
5776 KB |
Output is correct |
20 |
Correct |
88 ms |
5776 KB |
Output is correct |
21 |
Correct |
92 ms |
5776 KB |
Output is correct |
22 |
Correct |
92 ms |
5776 KB |
Output is correct |
23 |
Correct |
76 ms |
5776 KB |
Output is correct |
24 |
Correct |
96 ms |
5776 KB |
Output is correct |
25 |
Correct |
64 ms |
5776 KB |
Output is correct |
26 |
Correct |
84 ms |
5776 KB |
Output is correct |
27 |
Correct |
92 ms |
5776 KB |
Output is correct |
28 |
Correct |
80 ms |
5776 KB |
Output is correct |
29 |
Correct |
76 ms |
5776 KB |
Output is correct |
30 |
Correct |
92 ms |
5776 KB |
Output is correct |