62492023-11-08 23:21:32CWMDiana and Numberscpp17Elfogadva 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";*/
		}
	}
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva3ms1688 KiB
subtask29/9
2Elfogadva3ms1924 KiB
3Elfogadva3ms2188 KiB
subtask312/12
4Elfogadva3ms2280 KiB
5Elfogadva3ms2492 KiB
6Elfogadva3ms2732 KiB
7Elfogadva3ms2944 KiB
8Elfogadva3ms3140 KiB
9Elfogadva3ms3152 KiB
subtask427/27
10Elfogadva28ms4324 KiB
subtask552/52
11Elfogadva14ms3604 KiB
12Elfogadva32ms3800 KiB
13Elfogadva30ms4912 KiB
14Elfogadva32ms5236 KiB
15Elfogadva23ms4552 KiB
16Elfogadva30ms5484 KiB
17Elfogadva28ms5640 KiB
18Elfogadva21ms4868 KiB
19Elfogadva21ms5688 KiB
20Elfogadva28ms5680 KiB
21Elfogadva3ms4720 KiB
22Elfogadva3ms4724 KiB
23Elfogadva3ms4848 KiB
24Elfogadva3ms4960 KiB
25Elfogadva3ms4932 KiB
26Elfogadva3ms4940 KiB