#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } //math //------------------------------------------- template inline T sqr(T x) { return x * x; } //typedef //------------------------------------------ typedef vector VI; typedef vector VVI; typedef vector VS; typedef pair PII; typedef long long LL; typedef vector VLL; //container util //------------------------------------------ #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(t,c) for(typeof((c).begin()) t=(c).begin(); t!=(c).end(); ++t) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) //repetition //------------------------------------------ #define FOR(t,a,b) for(int t=(a);t<(b);++t) #define REP(t,n) FOR(t,0,n) //constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); //clear memory #define CLR(a) memset((a), 0 ,sizeof(a)) //debug #define dump(x) cerr << #x << '=' << (x) << endl; #define debug(x) cerr << #x << '=' << (x) << '('<<'L' << __LINE__ << ')' << ' ' << __FILE__ << endl; const LL mod = 1000000007; LL gcd(LL a, LL b) { if (a%b == 0) { return b; } else return gcd(b, a%b); } LL lcm(LL a, LL b) { return a / gcd(a, b)*b; } #define Yes_ cout<<"Yes"<> s; int n = SZ(s); VI numA(n); VI numB(n),diff(n); REP(i, SZ(s)) { if (i > 0) { numA[i] = numA[i - 1]; numB[i] = numB[i - 1]; } if (s[i] == 'A') { numA[i]++; } else { numB[i]++; } diff[i] = numA[i] - numB[i]; } int m = 0; int ans = 0; FOR(start,0, SZ(s)){ int d = diff[start];//いまAがBよりdiff個多ければ //何もしなくても数が同じ if (d == 0) { debug(start+1); if (start+1 > m) { m = start+1; ans = 1; } else if (d == m) { ans++; } } for (int end = 0; end < start; end++) { //cerr << start << " " << end << " " << d <<" "<< diff[end] << endl; if (diff[end] == d) {//過去にAがBよりdiff個多かったところを探してその分を引く int a = start - end; debug(a); if (a > m) { m = a; ans = 1; } else if (a == m) { ans ++; } } } } cout << m << endl; }