链接:https://codeforces.com/contest/1141/problem/D
题意:
给连个n长度的字符串。
求两个字符串相同字符对应位置的对数,并挨个打印。
字符:?可以代替任何字符。
思路:
对第一个字符串建立字符与位置的映射。
再处理第二个字符。
同时第二个字符中的‘?'不做处理。
全部遍历完了以后,再处理?的情况。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
map<char, queue<int> > M;
vector<pair<int, int> > res;
queue<int> other;
int main()
{
int n;
cin >> n;
string a, b;
cin >> a;
for (int i = 0;i < n;i++)
M[a[i]].push(i + 1);
cin >> b;
for (int i = 0;i < n;i++)
{
if (b[i] != '?')
{
if (M[b[i]].size() > 0)
{
res.push_back(make_pair(M[b[i]].front(), i + 1));
M[b[i]].pop();
}
else if (M['?'].size() > 0)
{
res.push_back(make_pair(M['?'].front(), i + 1));
M['?'].pop();
}
}
else
other.push(i + 1);
}
for (int i = 0;i <= 25;i++)
{
while (M[(char)(i + 'a')].size() && other.size())
{
res.push_back(make_pair(M[(char)(i + 'a')].front(), other.front()));
M[(char)(i + 'a')].pop();
other.pop();
}
}
while (M['?'].size() && other.size())
{
res.push_back(make_pair(M['?'].front(), other.front()));
M['?'].pop();
other.pop();
}
cout << res.size() << endl;
for (auto x : res)
cout << x.first << ' ' << x.second << endl;
return 0;
}