Codeforces1243B2 STL字符串模拟

https://codeforces.com/contest/1243/problem/B2
最多遍历时将1对交换2次
注意 s[i]和t[i]相等时 不用交换 浪费次数增加次数
交换至多2n次
1=<m<=2
n
每次交换s[i]与t[j] 最终使得s t串相等
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca

Yes
1
1 4
No
No
Yes
3
1 2
3 1
2 3

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int maxn=1e6+5;
ll q,a[maxn],f,ans,k,n;
string s,t;
int main(){
	cin>>k;
	while(k--){
		cin>>n>>s>>t;
		f=1;

		vector<ll> cnt(26);
		vector<pll> v;
		for(int i=0;i<n;i++) {
			cnt[s[i]-'a']++;
			cnt[t[i]-'a']++;
		}
		for(int i=0;i<26;i++) {
			if(cnt[i]&1){
				f=0;
				break;
			}
		}
		if(!f){
			cout<<"No"<<endl;
			continue;
		}
		cout<<"Yes"<<endl;
		for(int i=0;i<n;i++){
			if(s[i]!=t[i]){
				for(int j=i+1;j<n;j++){
					if(s[i]==s[j]){
						swap(s[j],t[i]);	//s[j] 与 t[j] 一样 
						v.pb(mp(j,i));
						break; 
					}
					if(s[i]==t[j]){
						swap(s[j],t[j]);
						swap(s[j],t[i]);
						v.pb(mp(j,j));
						v.pb(mp(j,i));
						break;
					}
				}
			}
		}
		
		cout<<v.size()<<endl;
		for(auto k:v){
			cout<<k.fi+1<<" "<<k.se+1<<endl;
		}
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
 
const int maxn=1e6+5;
ll q,a[maxn],f,ans,k,n;
string s,t;
int main(){
	cin>>k;
	while(k--){
		cin>>n>>s>>t;
		f=1;
 
		vector<ll> cnt(26);
		vector<pll> v;
		for(int i=0;i<n;i++) {
			cnt[s[i]-'a']++;
			cnt[t[i]-'a']++;
		}
		for(int i=0;i<26;i++) {
			if(cnt[i]&1){
				f=0;
				break;
			}
		}
		if(!f){
			cout<<"No"<<endl;
			continue;
		}
		cout<<"Yes"<<endl;
		for(int i=0;i<n;i++){
			if(s[i]!=t[i]){
				int j=i+1;
				while(s[i]!=s[j] && j<n){
					j++;
				}
				if(j<n){	//ccb
							//aba
					swap(s[j],t[i]);		//bcb
					v.push_back(mp(j,i));	//aca
					continue;
				}
				j=i+1;
				while(s[i]!=t[j] && j<n){	//acb  aca  acc
											//cba  cbb  abb
					j++;
				}
				if(j<n){
					swap(s[j],t[j]);
					swap(s[j],t[i]);
					v.push_back(mp(j,j));
					v.push_back(mp(j,i));
				}
			}
		}
		cout<<v.size()<<endl;
		for(auto k:v){
			cout<<k.fi+1<<" "<<k.se+1<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值