牛客小白月赛66-B-再交换

这题我只能说有点逆天,看着简单,实现的时候莫名麻烦,做的非常不舒服

写一下实现思路

在1 -> n - 1 中

(1) 出现a[i] > b[i]  直接输出换

下面的分类讨论有点不好想

如果一直是a[i] == b[i],也就是前n - 1个元素都相等,此时

(1) a[i] > b[i]  交换 n n

(2) a[i] < b[i] 此时除n n外随便输出一个

如果存在a[i] < b[i]在n-1中,那么直接输出n n

这题做起来突出一个别扭,怎么做都不得劲,都感觉麻烦

心态方面有待加强

// Problem: 再交换
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/51137/B
// Memory Limit: 524288 MB
// Time Limit: 4000 ms
// Date: 2024-03-13 20:27:27
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define endl '\n'
#define int int64_t
using namespace std;
void solve() {
	int n; cin >> n;
	string a, b; cin >> a >> b;
	a = '!' + a;
	b = '!' + b;
    bool less = false;
	for (int i = 1; i < n; ++i) {
		if (a[i] > b[i]) {
			cout << i << " " << i << endl;
			return;
		}
        if(a[i] < b[i]) less = true;
	}
	if(less){
        cout << n <<" " << n << endl;
    }
    else{
        if(a[n] < b[n]){
            cout << 1 <<" " << 1<<endl;
        }
        else{
            cout << n <<" " << n << endl;
        }
    }
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值