【题解】P10244 String Minimization

P10244 String Minimization

题意

给你四个长度为 \(n\) 的字符串,分别是 \(abcd\)

你可以选择一个 \(i\) 然后交换 \(a[i]\)\(c[i]\),并交换 \(b[i]\)\(d[i]\)

求在 \(a\) 的字典序尽量小的前提下,\(b\) 字典序最小是什么。

思路

本题并不难。

只需要在 \(a[i]>c[i]\) 时,交换 \(a[i]\)\(c[i]\),并交换 \(b[i]\)\(d[i]\)

\(a[i]=c[i]\) 时,如果 \(b[i]>d[i]\),交换。

否则不换。

另外,交换的过程可以用 swap 函数解决。

代码

#include <bits/stdc++.h>
using namespace std;
int n;
string a,b,c,d;
int main(){    
    cin>>n>>a>>b>>c>>d;
    for(int i=0;i<n;i++){
    	if(a[i]>c[i]){
    		swap(b[i],d[i]);
    		swap(a[i],c[i]);
		}else if(a[i]==c[i]){
			if(b[i]>d[i]){
    		    swap(b[i],d[i]);
    		    swap(a[i],c[i]);
			}
		}
	}
//	cout<<a<<"\n";
	cout<<b;
    return 0; 
}
posted @ 2024-04-06 17:57  Kcjhfqr  阅读(49)  评论(0)    收藏  举报
.poem-wrap { position: relative; width: 1000px; max-width: 80%; border: 2px solid #797979; border-top: none; text-align: center; margin: 40px auto; } .poem-left { left: 0; } .poem-right { right: 0; } .poem-border { position: absolute; height: 2px; width: 27%; background-color: #797979; } .poem-wrap p { width: 70%; margin: auto; line-height: 30px; color: #797979; } .poem-wrap h1 { position: relative; margin-top: -20px; display: inline-block; letter-spacing: 4px; color: #797979; font-size: 2em; margin-bottom: 20px; } #poem_sentence { font-size: 25px; } #poem_info { font-size: 15px; margin: 15px auto; }