N_O_E_L's blog

By N_O_E_L, history, 4 years ago, In English

I have been trying to solve [problem:https://mirror.codeforces.com/contest/1281/problem/B] and couldn't solve it in the first step. So I took help from the tutorial and implemented the idea in my own way. But it got the wrong answer as a verdict. So I looked at the code of ecnerwala and found that the idea is same and now I can't figure out what is wrong with my code. Can anyone help me to find it out? I couldn't find a better way than posting this and seeking your help.

Thank You

My code :

[submission:86280675]

#include <bits/stdc++.h>

using namespace std;

void solve(void)
{
	string a, b;
	cin >> a >> b;

	if(a<b) {cout << a << "\n"; return ;}
	string k = a;
	sort(k.begin(), k.end());
	int n = a.length();
	bool ok = true;
	for(int i = 0; i<n && ok; i++){
		if(a[i] != k[i]){
			for(int j = i+1; j<n; j++){
				if(a[j] == k[i]){
					swap(a[j], a[i]);
					ok = false;
					break;
				}
			}
		}
	}
	if(a<b) cout << a << "\n";
	else cout << "---\n";
}

int main(void)
{
    int t;

    cin >> t;

    while(t--){
    	solve();
    }

	return 0;
}

Ecnerwala's code :

[submission:66933725]

#include<bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T; cin >> T;
	while (T--) {
		string S, C; cin >> S >> C;
		if(S<C) {cout << S << "\n"; continue;}
		for (int i = 0; i < int(S.size()); i++) {
			char best = *min_element(S.begin()+i, S.end());
			if (best == S[i]) continue;
			for (int j = int(S.size()) - 1; j > i; j--) {
				if (S[j] == best) {
					swap(S[j], S[i]);
					goto done;
				}
			}
		}
done:
		if (S < C) {
			cout << S << '\n';
		} else {
			cout << "---" << '\n';
		}
	}

	return 0;
}

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it

By N_O_E_L, history, 5 years ago, In English

We want more div.4 contests. Because, for the newbies div.4 problems helps a lot to gain the basics and all. Besides, some of us are so new in this field that they rarely can solve 2-3 problems of div.3. So div.4 can help to keep their moral up.

TIA,

N_O_E_L

Full text and comments »

  • Vote: I like it
  • -34
  • Vote: I do not like it