codeforces 1141 D 模拟
xg
太久没写模拟,总是写一般觉得纰漏太多
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 150000+ 100; vector<int>v1[30],v2[30]; int vis1[maxn],vis2[maxn]; char a[maxn],b[maxn]; int main() { int n; cin>>n; queue<int>que1,que2; rep(i,1,n){ char c; cin>>c; a[i] = c; if(c-'a' == -34) { que1.push(i); continue; } v1[c-'a'].pb(i); } rep(i,1,n){ char c; cin>>c; b[i] = c; if(c-'a' == -34) { que2.push(i); continue; } v2[c-'a'].pb(i); } queue<pair<int,int> > ans; rep(i,0,25){ int minn = min(v1[i].size(),v2[i].size()); for(int j = 0; j < minn;++j){ /* vis1[v1[i][j]] = 1; vis2[v2[i][j]] = 1;*/ ans.push(mp(v1[i][j],v2[i][j])); } if(v2[i].size() > v1[i].size()){ while(1){ if(minn == v2[i].size() || que1.size() ==0){ break; } int tmp = que1.front();que1.pop(); ans.push(mp(tmp,v2[i][minn])); minn++; } } else { while(1){ if(minn == v1[i].size() || que2.size() ==0){ break; } int tmp = que2.front();que2.pop(); ans.push(mp(v1[i][minn],tmp)); minn++; } } } while(1){ if(que1.size() == 0 || que2.size() == 0) break; int x = que1.front();que1.pop(); int y = que2.front();que2.pop(); ans.push(mp(x,y)); } cout<<ans.size()<<endl; while(ans.size()){ int x = ans.front().fi; int y = ans.front().se; ans.pop(); cout<<x<<sp<<y<<endl; } }