【题解】CF1971B Different String

题解:CF1971B Different String

题意

给予你一个字符串 \(s\),保证 \(s\) 的长度小于等于 \(10\)

任意排列 \(s\),使其与原 \(s\) 不同。

判断是否可以完成。

思路

由于字符串的长度最多为 \(10\),我们可以尝试字符串的两个字符的所有交换。复杂度 \(O(|s|^{2})\),这足够快了。如果它们都没有创建不同的字符串,那么原始字符串中的所有字符都是相同的,所以答案是否定的。

代码

#include<bits/stdc++.h>
using namespace std;
string s;
int t,k=0;
int main() {
	cin>>t;
	while(t--) {
		k=0;
		cin>>s;
		for(int i=1;i<s.size();i++){
			if(s[i]!=s[0]){
				swap(s[i],s[0]);
				k=1;
				break;
			}
		}
		if(!k){
			cout<<"NO\n";
		}else{
			cout<<"YES"<<"\n";
			cout<<s<<"\n";
		}
	}
	return 0;
}
posted @ 2024-05-16 17:26  Kcjhfqr  阅读(26)  评论(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; }