62492023-11-08 23:21:32CWMDiana and Numberscpp17Accepted 100/10032ms5688 KiB
#include <iostream>
#include <string>

using namespace std;

string largerString(string a, string b) {
	int idxa = 0;
	int idxb = 0;
	while (idxa<a.size() && a[idxa] == '0') {
		idxa++;
	}
	while (idxb < b.size() && b[idxb] == '0') {
		idxb++;
	}
	a.erase(0, idxa);
	b.erase(0, idxb);
	if (a.size() > b.size()) return a;
	else if (b.size() > a.size()) return b;
	else {
		for (size_t i = 0; i < a.size(); i++)
		{
			if (a[i] > b[i]) return a;
			if (b[i] > a[i]) return b;
		}
	}
	return a;
}
bool validString(string s) {
	for (size_t i = 0; i < s.size(); i++)
	{
		if (s[i] != '0') return true;
	}
	return false;
}
string RemoveWorstChar(string input, int divBranch) {
	int changeVal1 = -10;
	int lr1 = 100'001;
	int lr1Index = -1;
	for (size_t j = 0; j < input.size(); j++)
	{
		int val = input[j] - '0';
		if (val % 3 == divBranch) {
			if (j == input.size() - 1) {
				if ((changeVal1 <= 0 || lr1>1) /*&& (lr1Index==-1 || input[lr1Index]>=input[j])*/) {
					lr1 = 1;
					lr1Index = j;
					changeVal1 = 0;
				}
			}
			for (size_t k = j + 1; k < input.size(); k++)
			{
				if (j > 0 || input[k] != '0') {
					int curlr = k - j;
					int curCV = input[k] - input[j];
					if (curlr == 1 && curCV > 0) {
						changeVal1 = curCV;
						lr1 = 1;
						lr1Index = j;
						j = input.size();
						k = input.size();
					}
					else if (curlr == 1 && changeVal1<=0) {
						changeVal1 = curCV;
						lr1 = 1;
						lr1Index = j;
					}
					else if (curlr < lr1) {
						lr1 = curlr;
						changeVal1 = curCV;
						lr1Index = j;
					}
					else if (curlr == lr1 && curCV >= changeVal1) {
						changeVal1 = curCV;
						lr1Index = j;
					}
					break;
				}
				else if (k == input.size() - 1 && lr1 == 100'001) return "0";
			}
		}
		//cout << lr1Index << " " << changeVal1 << " " << lr1 << "\n";
	}
	if (lr1 != 100'001) {
		input.erase(lr1Index, lr1);
	}
	return input;
}
bool DivBy3(string input) {
	int sum = 0;
	for (size_t i = 0; i < input.size(); i++)
	{
		sum += input[i] - '0';
	}
	if (sum % 3 == 0) return true;
	return false;
}
bool BruteForce(string input, string result) {
	const string inp = input;
	string bestSoFar = "0";
	if (DivBy3(input)) {
		bestSoFar = largerString(bestSoFar, input);
	}
	for (size_t i = 0; i < inp.size(); i++)
	{
		input = inp;
		input.erase(i,1);
		if (DivBy3(input)) {
			bestSoFar = largerString(bestSoFar, input);
		}
	}
	for (size_t i = 0; i < inp.size(); i++)
	{
		for (size_t j = i; j < inp.size()-1; j++)
		{
			input = inp;
			input.erase(i,1);
			input.erase(j,1);
			if (DivBy3(input)) {
				bestSoFar = largerString(bestSoFar, input);
			}
		}
	}
	if (result == bestSoFar) return true;
	return false;
}

int main()
{
	int a;
	cin >> a;
	for (size_t i = 0; i < a; i++)
	{
		string input;
		cin >> input;
		/*string input = to_string(i);*/
		int inpSum = 0;
		int oneLeft = 0;
		int twoLeft = 0;
		for (size_t j = 0; j < input.size(); j++)
		{
			inpSum += input[j] - '0';
			if (input[j] % 3 == 1) oneLeft++;
			else if (input[j] % 3 == 2) twoLeft++;
		}
		inpSum %= 3;
		// 0
		if (inpSum == 0) {
			cout << input << "\n";
			continue;
		}
		// 1
		if (inpSum == 1) {
			string res1 = "0";
			string res2 = "0";
			if (oneLeft > 0) {
				res1 = RemoveWorstChar(input, 1);
			}
			//1.2
			if (twoLeft > 1 && input.size()-res1.size()>1) {
				res2 = RemoveWorstChar(input, 2);
				res2 = RemoveWorstChar(res2, 2);
			}
			string sol = largerString(res1, res2);
			if (validString(sol)) cout << sol << "\n";
			else cout << "-1\n";
			/*if (!validString(sol)) sol = "";
			if (!BruteForce(input, sol)) cout << input << "\n";*/
		}
		// 2
		if (inpSum == 2) {
			string res1 = "0";
			string res2 = "0";
			if (twoLeft > 0) {
				res1 = RemoveWorstChar(input, 2);
			}
			//1.2
			if (oneLeft > 1 && input.size() - res1.size() > 1) {
				res2 = RemoveWorstChar(input, 1);
				res2 = RemoveWorstChar(res2, 1);
			}
			string sol = largerString(res1, res2);
			if (validString(sol)) cout << sol << "\n";
			else cout << "-1\n";
			/*if (!validString(sol)) sol = "";
			if(!BruteForce(input, sol)) cout << input << "\n";*/
		}
	}
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1688 KiB
subtask29/9
2Accepted3ms1924 KiB
3Accepted3ms2188 KiB
subtask312/12
4Accepted3ms2280 KiB
5Accepted3ms2492 KiB
6Accepted3ms2732 KiB
7Accepted3ms2944 KiB
8Accepted3ms3140 KiB
9Accepted3ms3152 KiB
subtask427/27
10Accepted28ms4324 KiB
subtask552/52
11Accepted14ms3604 KiB
12Accepted32ms3800 KiB
13Accepted30ms4912 KiB
14Accepted32ms5236 KiB
15Accepted23ms4552 KiB
16Accepted30ms5484 KiB
17Accepted28ms5640 KiB
18Accepted21ms4868 KiB
19Accepted21ms5688 KiB
20Accepted28ms5680 KiB
21Accepted3ms4720 KiB
22Accepted3ms4724 KiB
23Accepted3ms4848 KiB
24Accepted3ms4960 KiB
25Accepted3ms4932 KiB
26Accepted3ms4940 KiB