제출 #1093596

#제출 시각아이디문제언어결과실행 시간메모리
1093596IBoryFlip it and Stick it (CCO23_day2problem1)C++17
18 / 25
34 ms10432 KiB
#include <bits/stdc++.h>
using namespace std;

int Count(string S, string T) {
	int ret = 0;
	for (int i = 0; i + T.size() <= S.size(); ++i) {
		ret += (S.substr(i, T.size()) == T);
	}
	return ret;
}

int Solve(string S, string T) {
	if (T.size() == 1) return (S.find(T) == string::npos ? 0 : -1);
	int N = S.size();

	if (T == "011" || T == "100") {
		reverse(S.begin(), S.end());
		reverse(T.begin(), T.end());
	}
	if (T[0] == '1') {
		for (char& c : S) c ^= 1;
		for (char& c : T) c ^= 1;
	}

	int zero = 0;
	for (char c : S) zero += (c == '0');

	if (T == "01") return Count(S, T);
	if (T == "00") {
		int lim = (N + 1) / 2;
		if (lim < zero) return -1;
		return Count(S, T);
	}

	if (T == "001") return Count(S, T);
	if (T == "010") return (Count(S, T) + 1) / 2;
	if (T == "000") {
		int lim = N - N / 3;
		if (lim < zero) return -1;
		
		int ret = 0;
		for (int t = 0; t < 2; ++t) {
			for (int i = 0; i + 3 <= N; ++i) {
				if (S.back() == '0') break;
				if (S[i] == '1' || S[i + 1] == '1' || S[i + 2] == '1') continue;
				ret++;
				reverse(S.begin() + i + 2, S.end());
				break;
			}
			reverse(S.begin(), S.end());
		}

		multiset<int> X;
		int cur = 0;
		for (char c : S) {
			if (c == '0') cur++;
			else {
				X.insert(cur);
				cur = 0;
			}
		}
		X.insert(cur);

		while (3 <= *X.rbegin()) {
			ret++;
			int a = *X.begin(), b = *X.rbegin();
			int n = min(2 - a, b - 2);
			X.erase(X.find(a));
			X.erase(X.find(b));
			X.insert(a + n);
			X.insert(b - n);
		}

		return ret;
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	string S, T;
	cin >> S >> T;
	cout << Solve(S, T);
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'int Solve(std::string, std::string)':
Main.cpp:76:1: warning: control reaches end of non-void function [-Wreturn-type]
   76 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...