1743D - Problem with Random Tests - CodeForces Solution


brute force probabilities

Please click on ads to support us..

Python Code:

def getStr(i, k):
    ans = ''
    while i < n:
        if s[i] == '1' or s[k] == '1': ans += '1'
        else: ans += '0'
        i += 1
        k += 1
    return ans

n = int(input())
s = input()
i = 0
while i < n and s[i] == '0': i += 1
if i == n:
    print(0)
    exit()
j = i
while i < n and s[i] == '1': i += 1
ans = '0'*(n-i-j)
for k in range(i, j-1, -1):
    t = getStr(i, k)
    ans = max(ans, t)
print(s[j:i] + ans)

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int main() {

	int n;
	string s;
	cin >> n >> s;

	int flag = 0;
	for (int i = 0; i < n; i ++ ) {
		if (s[i] == '1') {
			flag = 1;
			s = s.substr(i);
			break;
		}
	}

	if (!flag) {
		cout << "0\n";
		return 0;
	}

 	int idx = -1;
	for (int i = 0; i < s.size(); i ++ ) {
		if (s[i] == '0') {
			idx = i;
			break;
		}
	}

	if (idx == -1) {
		cout << s << endl;
		return 0;
	}

 	string res = s;
	for (int i = idx - 1; i >= 0; i -- ) {
		string sa = s.substr(i), sb = s.substr(idx);
		string tmp = s;
		for (int j = 0; j < sb.size(); j ++ ) {
			tmp[idx + j] = max(sa[j], sb[j]);
		}
  		res = max(res, tmp);
	}
	cout << res << endl;
	return 0;
}
	 		    	 	 	  	 	     		  	  	


Comments

Submit
0 Comments
More Questions

255A - Greg's Workout
1059A - Cashier
1389C - Good String
1561A - Simply Strange Sort
1337B - Kana and Dragon Quest game
137C - History
1443C - The Delivery Dilemma
6C - Alice Bob and Chocolate
1077C - Good Array
285B - Find Marble
6A - Triangle
1729A - Two Elevators
1729B - Decode String
1729C - Jumping on Tiles
1729E - Guess the Cycle Size
553B - Kyoya and Permutation
1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment
1144A - Diverse Strings
1553B - Reverse String
1073A - Diverse Substring
630N - Forecast
312B - Archer
34D - Road Map