【codeforces 19/11/06 div2】B.Character Swap

 1 #include<iostream>
 2 #include<map>
 3 #include<set>
 4 #include<string>
 5 #include<vector>
 6 using namespace std;
 7 
 8 string s, t;
 9 vector<pair<int, int>> ans;
10 
11 int main()
12 {
13     int T; 
14     cin >> T;
15     while (T--) 
16     {
17         int n;
18         cin >> n;
19         cin >> s >> t;
20         ans.clear();
21         for (int i = 0; i < n; i++) 
22         {
23             if (s[i] == t[i]) continue;
24             for (int j = i + 1; j < n; j++)
25             {
26                 if (t[i] == t[j])
27                 {
28                     swap(s[i], t[j]);
29                     ans.push_back(pair<int, int>(i, j));
30                 }
31                 else if (s[j] == t[i]) 
32                 {
33                     swap(s[j], t[j]);
34                     ans.push_back(pair<int, int>(j, j));
35                     swap(s[i], t[j]);
36                     ans.push_back(pair<int,int>(i, j));
37                 }
38                 if (s[i] == t[i]) break;
39             }
40             if (s[i] != t[i]) 
41             {
42                 ans.clear();
43                 break;
44             }
45         }
46         if (!ans.size())
47         {
48             cout << "No" << endl;
49         }
50         else 
51         {
52             cout << "Yes" << endl;
53             cout << ans.size() << endl;
54             for (int i = 0; i < ans.size(); i++)
55             {
56                 cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
57             }
58         }
59     }
60     return 0;
61 }
View Code

 

posted on 2019-11-07 17:10  thjkhdf12  阅读(184)  评论(0)    收藏  举报