题目传送门
思路
直接模拟,可以使用 std::unordered_map 进行映射,最后输出即可。
模拟过程如下:
- 对于每个 若为小写字母,则存入临时字符串 。
- 对于每个 若为数字,则使 。
- 对于 ,若其非数字,则表示当前的编号已经取完,将其插入到 ,并使 。
- 最后输出 和 。
代码
AC Code
#include <bits/stdc++.h>
#define rep(x, y, z) for (int x = (y); x <= z; ++x)
#define per(x, y, z) for (int x = (y); x >= z; --x)
using namespace std;
typedef long long ll;
unordered_map<int, string> mp1, mp2; // 开两个 unordered_map 来映射
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
string s1, s2;
cin >> s1 >> s2;
s1 = " " + s1;
s2 = " " + s2;
int x = s1.size();
int y = s2.size();
string curx;
int num = 0;
rep(i, 1, x)
{
if (islower(s1[i])) // 如果是小写字母
{
curx += s1[i]; // 存入小写字母
}
else if (isdigit(s1[i])) // 如果是数字
{
num = num * 10 + s1[i] - '0'; // 计算当前的编号
if (!isdigit(s1[i + 1])) // 不是数字代表已经取完
{
mp1.insert({num, curx}); // 映射
curx.clear(); // 注意要清空
num = 0; // 重置
}
}
}
string cury;
num = 0;
rep(i, 1, y)
{
if (islower(s2[i]))
{
cury += s2[i];
}
else if (isdigit(s2[i]))
{
num = num * 10 + s2[i] - '0';
if (!isdigit(s2[i + 1]))
{
mp2.insert({num, cury});
cury.clear();
num = 0;
}
}
}
rep(i, 1, n) cout << mp1[i] << mp2[i]; // 输出
return 0;
}时间复杂度
时间复杂度平均为 ,若哈希冲突则更坏,所以数据范围大时建议使用 std::map 或 std::vector。