forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE0055.cpp
More file actions
45 lines (41 loc) · 769 Bytes
/
E0055.cpp
File metadata and controls
45 lines (41 loc) · 769 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
35
36
37
38
39
40
41
42
43
44
45
// Problem Code: ABGAME
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <functional>
using namespace std;
char winner(string S) {
int a, b, count, XOR;
vector<int> nims;
a = b = 0;
for(int j, i=0 ; i<S.length() ; i++)
if(S[i] != '.') {
count = 0;
for(j = i+1 ; j < S.length() && S[j] == '.' ; j++)
count++;
if(j != S.length() && S[i] != S[j])
nims.push_back(count);
else {
if(S[i] == 'A')
a += count;
else
b += count;
}
i = j;
}
if(a != b)
return (a > b) ? 'A' : 'B';
XOR = accumulate(nims.begin(), nims.end(), 0, bit_xor<int>());
return (XOR) ? 'A' : 'B';
}
int main() {
int T;
string s;
cin >> T;
while(T--) {
cin >> s;
cout << winner(s) << endl;
}
return 0;
}