本文主要是介绍Codeforces Round 850 (Div. 2) D. Letter Exchange,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
思路:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
const int maxn = 1e6 + 5, inf = 1e9, maxm = 4e4 + 5;
const int N = 1e6;
// const int mod = 1e9 + 7;
const int mod = 998244353;
// int a[505][5005];
// bool vis[505][505];
// char s[505][505];
int a[maxn], b[maxn];
int vis[maxn];
string s;
int n, m;struct Node{int val, id;bool operator<(const Node &u)const{return val < u.val;}
};
// Node c[maxn];int ans[maxn];
int pre[maxn];//long long ? maxn ?char change(char ch){if(ch == 'w') return 'a';if(ch == 'i') return 'b';return 'c';
}char rechange(int id){if(id == 0) return 'w';if(id == 1) return 'i';return 'n';
}
void solve(){int res = 0;int q, k;int sum = 0;int mx = 0;cin >> n;// for(int i = 1; i <= n; i++){// cin >> a[i];// }vector<int> vec[3][3];for(int i = 1; i <= n; i++){string s;cin >> s;int cnt[3] = {0};int tot = 0;for(int j = 0; j < 3; j++){s[j] = change(s[j]);char ch = s[j];if(!cnt[ch - 'a']){tot++;}cnt[ch - 'a']++;}if(tot == 3) continue;if(tot == 1){int id = s[0] - 'a';for(int j = 0; j < 3; j++){if(j != id){vec[id][j].pb(i);// cout << i << ' ' << id << ' ' << j << '\n';}}}else{int more, less;for(int j = 0; j < 3; j++){if(cnt[j] == 2){more = j;}else if(!cnt[j]){less = j;}}vec[more][less].pb(i);}// cout << i << ' ' << tot << '\n';}vector<array<int, 4>> ans;for(int i = 0; i < 3; i++){int j = (i + 1) % 3;// cout << i << ' ' << j << ":\n";// for(auto x : vec[i][j]){// cout << x << ' ';// }// cout << '\n';// cout << j << ' ' << i << ":\n";// for(auto x : vec[j][i]){// cout << x << ' ';// }// cout << '\n';while(!vec[i][j].empty() && !vec[j][i].empty()){int x = vec[i][j].back(), y = vec[j][i].back();vec[i][j].pop_back();vec[j][i].pop_back();// cout << x << ' ' << rechange(i) << ' ' << y << rechange(j) << '\n';ans.pb({x, rechange(i), y, rechange(j)});}}while(!vec[0][1].empty()){int i = vec[0][1].back(), j = vec[1][2].back(), k = vec[2][0].back();vec[0][1].pop_back();vec[1][2].pop_back();vec[2][0].pop_back();ans.pb({i, rechange(0), j, rechange(1)});ans.pb({j, rechange(0), k, rechange(2)});}while(!vec[0][2].empty()){int i = vec[0][2].back(), j = vec[2][1].back(), k = vec[1][0].back();vec[0][2].pop_back();vec[2][1].pop_back();vec[1][0].pop_back();ans.pb({i, rechange(0), j, rechange(2)});ans.pb({j, rechange(0), k, rechange(1)});}cout << ans.size() << '\n';for(auto [x, c1, y, c2] : ans){cout << x << ' ' << char(c1) << ' ' << y << ' ' << char(c2) << '\n';}
}signed main(){ios::sync_with_stdio(0);cin.tie(0);int T = 1;cin >> T;while (T--){solve();}return 0;
}
这篇关于Codeforces Round 850 (Div. 2) D. Letter Exchange的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!