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

Compiler report

input/code.cpp: In function 'int slow(const string&)':
input/code.cpp:14:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < s.size() - 1; ++i) {
                  ~~^~~~~~~~~~~~~~
input/code.cpp: In function 'int brute(const string&)':
input/code.cpp:32:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < s.size(); ++i) {
                  ~~^~~~~~~~~~
input/code.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < s.size() - 1; ++i) {
                  ~~^~~~~~~~~~~~~~

Code

#include <algorithm>
#include <iostream>
#include <map>
#include <string>

using namespace std;

map<string, int> cache;

int slow(const string &s) {
	if (s.size() == 0) return 0;
	if (cache.find(s) != cache.end()) return cache[s];
	int maxways = 0;
	for (int i = 0; i < s.size() - 1; ++i) {
		if (s[i] != s[i+1]) {
			string s2 = s;
			s2.erase(i, 2);
			//auto asd = slow(s2) + 2;
			//if (asd < maxways) {
			//	cout << s << " -> " << s2 << " was bad" << endl;
			//}
			maxways = max(maxways, slow(s2) + 2);
		}
	}
	cache[s] = maxways;
	return maxways;
}

int brute(const string &s) {
	if (s.size() == 0) return 0;
	int counts[3] = {0, 0, 0};
	for (int i = 0; i < s.size(); ++i) {
		++counts[s[i]-'A'];
	}
	int maxind = distance(counts, max_element(counts, counts + 3));
	for (int i = 0; i < s.size() - 1; ++i) {
		if (s[i] != s[i+1] && (s[i] - 'A' == maxind || s[i+1] - 'A' == maxind)) {
			string s2 = s;
			s2.erase(i, 2);
			return brute(s2) + 2;
		}
	}
	return 0;
}

int main()
{
	//string in = "AAAAAAAABBB";
	//do {
	//	if (slow(in) != brute(in)) {
	//		cout << in << slow(in) << brute(in) << endl;
	//	}
	//} while(next_permutation(in.begin(), in.end()));
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		string s;
		cin >> s;
		cout << brute(s) << endl;
	}
}

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