Codeforces Round #547 (Div. 3) D
http://codeforces.com/contest/1141/problem/D
题目大意:
鞋子匹配,用一个小写字母表示一种颜色。L[i]表示左脚的颜色,R[i]表示右脚的颜色,只有当L[i]和R[j]的颜色差不多了,才算匹配成功。但是,有一种特殊的颜色‘?’,该颜色可以和任意另一半鞋子匹配。
思路:
取出‘?’,格外判断就好了
//看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> #pragma comment(linker,"/STACK:102400000,102400000") #define LL long long #define ALL(a) a.begin(), a.end() #define pb push_back #define mk make_pair #define fi first #define se second #define haha printf("haha\n") using namespace std; const int maxn = 150000 + 5; vector<pair<int, int> > l, r; bool fl[maxn], fr[maxn]; char ch[maxn]; int n; int main(){ cin >> n; scanf("%s", ch); vector<pair<int, int> > wenhaol; for (int i = 0; ch[i] != '\0'; i++){ if (ch[i] >= 'a' && ch[i] <= 'z') l.pb(mk(ch[i], i)); else if (ch[i] == '?') wenhaol.pb(mk(ch[i] + 80, i)); } sort(l.begin(), l.end()); scanf("%s", ch); vector<pair<int, int> > wenhaor; for (int i = 0; ch[i] != '\0'; i++){ if (ch[i] >= 'a' && ch[i] <= 'z') r.pb(mk(ch[i], i)); else if (ch[i] == '?') wenhaor.pb(mk(ch[i] + 80, i)); } sort(r.begin(), r.end()); vector<pair<int, int> > ans; int lb = 0, rb = 0; while (lb < l.size() && rb < r.size()){ //printf("lb = %d rb = %d\n", lb, rb); if (l[lb].fi == r[rb].fi){ ans.pb(mk(l[lb].se, r[rb].se)); fl[lb] = fr[rb] = 1; lb++, rb++; continue; } /*if (l[lb].fi == '?' + 80 || r[rb].fi == '?' + 80){ ans.pb(mk(l[lb].se, r[rb].se)); fl[lb] = fr[rb] = 1; lb++, rb++; continue; }*/ if (l[lb].fi > r[rb].fi && l[lb].fi != '?' + 80){ rb++; continue; } if (r[rb].fi > l[lb].fi && r[rb].fi != '?' + 80){ lb++; continue; } } lb = 0, rb = 0; for (int i = 0; i < l.size(); i++){ if (fl[i] != 1 && rb < wenhaor.size()){ ans.pb(mk(l[i].se, wenhaor[rb].se)); rb++; } } for (int i = 0; i < r.size(); i++){ if (fr[i] != 1 && lb < wenhaol.size()){ ans.pb(mk(wenhaol[lb].se, r[i].se)); lb++; } } while (lb < wenhaol.size() && rb < wenhaor.size()){ ans.pb(mk(wenhaol[lb].se, wenhaor[rb].se)); lb++, rb++; } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); i++){ printf("%d %d\n", ans[i].fi + 1, ans[i].se + 1); } return 0; }