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

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

»
4 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

Try the testcase:

1

BBBAA

ABBBA

Your code says that it is impossible whereas it should have been ABBAB

I have modified your code to give the correct result : here

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

My problem has been solved. Thank you ..