-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathP2679.c
More file actions
34 lines (30 loc) · 773 Bytes
/
P2679.c
File metadata and controls
34 lines (30 loc) · 773 Bytes
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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define maxn 1000000
#define GREEN 'G'
#define RED 'R'
#define NOT_A_LOC 0xffffffff
int loc[2 * maxn + 2];
int sum[maxn + 1];
char a[maxn + 1];
int hash(int index) { return index + maxn; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
scanf("%s", a + 1);
int n = strlen(a + 1);
int res = 0;
memset(loc, NOT_A_LOC, sizeof(loc));
loc[hash(0)] = 0;
for (int i = 1; i <= n; i++) {
if (i) sum[i] = sum[i - 1];
if (a[i] == GREEN) sum[i] += 1;
else sum[i] -= 1;
if (loc[hash(sum[i])] != NOT_A_LOC) {
res = max(res, i - loc[hash(sum[i])]);
}
else loc[hash(sum[i])] = i;
}
printf("%d", res);
return 0;
}