Help Needed ..

Правка en1, от N_O_E_L, 2020-07-08 18:16:29

I have been trying to solve [problem:https://codeforces.net/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 :

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 :

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

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский N_O_E_L 2020-07-08 18:16:29 1808 Initial revision (published)