CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:ABC-poisto
Sender:Olli
Submission time:2020-10-17 09:45:41 +0300
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED42
#2ACCEPTED58
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.01 s2details

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int N = 111;

string rot(string s) {
	int A, B, C;
	A = 0; B = 0; C = 0;
	int n = s.size();
	for(int i = 0; i < n; ++i) {
		if(s[i] == 'A') ++A;
		if(s[i] == 'B') ++B;
		if(s[i] == 'C') ++C;
	}
	if(A < B) {
		string t;
		for(int i = 0; i < n; ++i) {
			if(s[i] == 'A') t += 'B';
			if(s[i] == 'B') t += 'A';
			if(s[i] == 'C') t += 'C';
		}
		s = t;
		swap(A, B);
	}
	if(A < C) {
		string t;
		for(int i = 0; i < n; ++i) {
			if(s[i] == 'A') t += 'C';
			if(s[i] == 'B') t += 'B';
			if(s[i] == 'C') t += 'A';
		}
		s = t;
		swap(A, C);
	}
	if(B < C) {
		string t;
		for(int i = 0; i < n; ++i) {
			if(s[i] == 'A') t += 'A';
			if(s[i] == 'B') t += 'C';
			if(s[i] == 'C') t += 'B';
		}
		s = t;
		swap(B, C);
	}
	return s;
}

int main() {
	iostream::sync_with_stdio(false);
	cin.tie(0);
	int q;
	cin >> q;
	while(q > 0) {
		--q;
		string s;
		cin >> s;

		int ans = 0;

		while(true) {
			s =	rot(s);

			int n = s.size();

			int A, B, C;
			A = 0; B = 0; C = 0;
			for(int i = 0; i < n; ++i) {
				if(s[i] == 'A') ++A;
				if(s[i] == 'B') ++B;
				if(s[i] == 'C') ++C;
			}
			if(B == 0) break;
			bool w = false;
			for(int i = 0; i < n-1; ++i) {
				if((s[i] == 'A' && s[i+1] == 'B') || (s[i] == 'B' && s[i+1] == 'A')) {
					string t;
					for(int j = 0; j < n; ++j) {
						if(j == i || j == i+1) continue;
						t += s[j];	
					}
					s = t;
					++ans;
					w = true;
					break;
				}
			}
			if(w) continue;
			for(int i = 0; i < n-1; ++i) {
				if((s[i] == 'A' && s[i+1] == 'C') || (s[i] == 'C' && s[i+1] == 'A')) {
					string t;
					for(int j = 0; j < n; ++j) {
						if(j == i || j == i+1) continue;
						t += s[j];	
					}
					s = t;
					++ans;
					w = true;
					break;
				}

			}
		}
		cout << 2*ans << "\n";
	}
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
100
CABC
BABCCBCA
CBBCBBAC
ACAA
...

correct output
4
8
8
2
2
...

user output
4
8
8
2
2
...
Truncated

Test 2

Group: 2

Verdict: ACCEPTED

input
100
CCAAACBCBBCCACBBBCCACCCBABBCAB...

correct output
48
4
4
96
70
...

user output
48
4
4
96
70
...
Truncated