B1. Character Swap (Easy Version)
This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.
After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.
Ujan has two distinct strings ss and tt of length nn consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions ii and jj (1≤i,j≤n1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj. Can he succeed?
Note that he has to perform this operation exactly once. He has to perform this operation.
The first line contains a single integer kk (1≤k≤101≤k≤10), the number of test cases.
For each of the test cases, the first line contains a single integer nn (2≤n≤1042≤n≤104), the length of the strings ss and tt.
Each of the next two lines contains the strings ss and tt, each having length exactly nn. The strings consist only of lowercase English letters. It is guaranteed that strings are different.
For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise.
You can print each letter in any case (upper or lower).
4 5 souse houhe 3 cat dog 2 aa az 3 abc bca
Yes No No No
In the first test case, Ujan can swap characters s1s1 and t4t4, obtaining the word "house".
In the second test case, it is not possible to make the strings equal using exactly one swap of sisi and tjtj.
#include <bits/stdc++.h> #include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath> using namespace std; #define mem(s,t) memset(s,t,sizeof(s)) #define pq priority_queue #define pb push_back #define fi first #define se second #define ac return 0; #define ll long long #define cin2(a,n,m) for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j]; #define rep_(n,m) for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) #define rep(n) for(int i=1;i<=n;i++) #define test(xxx) cout<<" Test " <<" "<<xxx<<endl; #define TLE std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); #define lc now<<1 #define rc now<<1|1 #define ls now<<1,l,mid #define rs now<<1|1,mid+1,r #define half no[now].l+((no[now].r-no[now].l)>>1) #define ll long long const int mxn = 1e6+5; ll n,m,k,ans,cnt,col; int a[mxn],b[27]; //这个题a【4】就够用 string str ,ch ; int main() { cin>>n; while(n--) { cin>>k; cin>>str>>ch; k = 0; memset(b,0,sizeof(b)); for(int i=0;i<str.size() && k<3;i++) { b[str[i]-'a']++; if(str[i]!=ch[i]) { a[++k]=i+1; } } if(k>=3 || k==1 ) cout<<"No"<<endl; else if(k==2) { if(str[a[1]-1] == str[a[2]-1] && ch[a[2]-1] == ch[a[1]-1] ) cout<<"Yes"<<endl; else cout<<"No"<<endl; } } return 0; }
B2. Character Swap (Hard Version)
This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In addition, k≤1000,n≤50k≤1000,n≤50 and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.
After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.
Ujan has two distinct strings ss and tt of length nn consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation at most 2n2n times: he takes two positions ii and jj (1≤i,j≤n1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj.
Ujan's goal is to make the strings ss and tt equal. He does not need to minimize the number of performed operations: any sequence of operations of length 2n2n or shorter is suitable.
The first line contains a single integer kk (1≤k≤10001≤k≤1000), the number of test cases.
For each of the test cases, the first line contains a single integer nn (2≤n≤502≤n≤50), the length of the strings ss and tt.
Each of the next two lines contains the strings ss and tt, each having length exactly nn. The strings consist only of lowercase English letters. It is guaranteed that strings are different.
For each test case, output "Yes" if Ujan can make the two strings equal with at most 2n2n operations and "No" otherwise. You can print each letter in any case (upper or lower).
In the case of "Yes" print mm (1≤m≤2n1≤m≤2n) on the next line, where mm is the number of swap operations to make the strings equal. Then print mm lines, each line should contain two integers i,ji,j (1≤i,j≤n1≤i,j≤n) meaning that Ujan swaps sisi and tjtj during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than 2n2n is suitable.
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
分析:问长度为n的两个字符串是否可以在2n次交换的操作中相同,
Code:如果某字符的个数不能被2整除,输出No。
否则,遍历S1,如果S1【i】!=S2【i】,从遍历后面的字符S1【j】,如果S1【i】==S1【j】保存i,j;如果S1【i】==S2【j】,保存并交换S1【j】s2【j】,最后输出就好了
C. Tile Painting:
https://www.cnblogs.com/Shallow-dream/p/11823678.html