62172023-11-08 10:33:26CWMDiana and Numberscpp17Hibás válasz 0/10034ms5704 KiB
#include <iostream>

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;
}

int main()
{
	int a;
	cin >> a;
	for (size_t i = 0; i < a; i++)
	{
		string input;
		cin >> input;
		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) {
			int changeVal1 = -10;
			int changeVal2 = -10;
			int changeVal3 = -10;
			int lr1 = 100'001;
			int lr2 = 100'001;
			int lr3 = 100'001;
			int lr1Index = -1;
			int lr2Index = -1;
			int lr3Index = -1;
			if (oneLeft > 0) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 1) {
						if (j == input.size()-1) {
							if (changeVal1 < 0) {
								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 < lr1) {
									lr1 = curlr;
									changeVal1 = curCV;
									lr1Index = j;
								}
								else if (curlr == lr1 && curCV >= changeVal1) {
									changeVal1 = curCV;
									lr1Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr1Index << " " << changeVal1 << " " << lr1 << "\n";
			}
			//1.2
			if (twoLeft > 1 && lr1>1) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 2) {
						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) {
									if (lr2 != 1 || changeVal2 < 0) {
										changeVal3 = changeVal2;
										lr3 = lr2;
										lr3Index = lr2Index;
										changeVal2 = curCV;
										lr2 = 1;
										lr2Index = j;
									}
									else if (lr3 != 1 || changeVal3 < 0) {
										changeVal3 = curCV;
										lr3 = 1;
										lr3Index = j;
									}
								}
								else if (curlr < lr2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr < lr3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								else if (curlr == lr2 && curCV >= changeVal2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr == lr3 && curCV >= changeVal3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr2Index << " " << changeVal2 << " " << lr2 << "\n";
				//cout << lr3Index << " " << changeVal3 << " " << lr3 << "\n";
			}
			//compare!
			string oldInput = input;
			if (lr1 != 100'001) {
				input.erase(lr1Index, lr1);
			}
			string res1 = input;
			input = oldInput;
			if (lr3 != 100'001) {
				input.erase(lr3Index, lr3);
			}
			if (lr2 != 100'001) {
				input.erase(lr2Index, lr2);
			}
			string res2 = input;
			input = oldInput;
			if (res1 == oldInput) {
				res1 = "0";
			}
			if (res2 == oldInput) {
				res2 = "0";
			}
			string sol = largerString(res1, res2);
			if (validString(sol)) cout << sol << "\n";
			else cout << "-1\n";
		}
		// 2
		if (inpSum == 2) {
			int changeVal1 = -10;
			int changeVal2 = -10;
			int changeVal3 = -10;
			int lr1 = 100'001;
			int lr2 = 100'001;
			int lr3 = 100'001;
			int lr1Index = -1;
			int lr2Index = -1;
			int lr3Index = -1;
			if (twoLeft > 0) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 2) {
						if (j == input.size() - 1) {
							if (changeVal1 < 0) {
								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 < lr1) {
									lr1 = curlr;
									changeVal1 = curCV;
									lr1Index = j;
								}
								else if (curlr == lr1 && curCV >= changeVal1) {
									changeVal1 = curCV;
									lr1Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr1Index << " " << changeVal1 << " " << lr1 << "\n";
			}
			//2.2
			if (oneLeft > 1 && lr1 > 1) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 1) {
						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) {
									if (lr2 != 1 || changeVal2 < 0) {
										changeVal3 = changeVal2;
										lr3 = lr2;
										lr3Index = lr2Index;
										changeVal2 = curCV;
										lr2 = 1;
										lr2Index = j;
									}
									else if (lr3 != 1 || changeVal3 < 0) {
										changeVal3 = curCV;
										lr3 = 1;
										lr3Index = j;
									}
								}
								else if (curlr < lr2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr < lr3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								else if (curlr == lr2 && curCV >= changeVal2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr == lr3 && curCV >= changeVal3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr2Index << " " << changeVal2 << " " << lr2 << "\n";
				//cout << lr3Index << " " << changeVal3 << " " << lr3 << "\n";
			}
			//compare!
			string oldInput = input;
			if (lr1 != 100'001) {
				input.erase(lr1Index, lr1);
			}
			string res1 = input;
			input = oldInput;
			if (lr3 != 100'001) {
				input.erase(lr3Index, lr3);
			}
			if (lr2 != 100'001) {
				input.erase(lr2Index, lr2);
			}
			string res2 = input;
			input = oldInput;
			if (res1 != oldInput) {
				cout << res1 << " ";
			}
			if (res2 != oldInput) {
				cout << res2;
			}
			cout << "\n";
		}
	}
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva3ms1820 KiB
subtask20/9
2Hibás válasz3ms2060 KiB
3Hibás válasz3ms2268 KiB
subtask30/12
4Hibás válasz3ms2516 KiB
5Hibás válasz3ms2720 KiB
6Futási hiba4ms3484 KiB
7Hibás válasz3ms3040 KiB
8Hibás válasz3ms2936 KiB
9Hibás válasz3ms3048 KiB
subtask40/27
10Hibás válasz28ms4584 KiB
subtask50/52
11Hibás válasz16ms3516 KiB
12Hibás válasz32ms3760 KiB
13Elfogadva34ms5120 KiB
14Elfogadva34ms5096 KiB
15Futási hiba17ms4212 KiB
16Elfogadva32ms5108 KiB
17Hibás válasz28ms5392 KiB
18Hibás válasz23ms4232 KiB
19Hibás válasz23ms5468 KiB
20Elfogadva30ms5704 KiB
21Hibás válasz3ms4228 KiB
22Hibás válasz3ms4504 KiB
23Futási hiba4ms5016 KiB
24Hibás válasz3ms4452 KiB
25Hibás válasz3ms4452 KiB
26Hibás válasz3ms4580 KiB