// yukicoder: No.672 最長AB列 // 2019.5.2 bal4u #include #include #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int ins(char *s) // 文字列の入力 スペース以下の文字で入力終了 { char *p = s; do *s = gc(); while (*s++ > ' '); *--s = 0; return s - p; } #define MAX 200005 char S[MAX]; int w; int f[MAX<<1]; int main() { int i, a, max; w = ins(S+2)+2; max = 0, a = MAX, f[a] = 1; for (i = 2; i < w; i++) { if (S[i] == 'A') a++; else a--; if (f[a] == 0) f[a] = i; else if (i - f[a] > max) max = i - f[a]; } printf("%d\n", max); return 0; }