·题意
·输入铺面每个音符的时间点和轨道,输出整个铺面,当同一时间点存在两个音符时将它们连在一起;
·分析
·构建铺面的过程我们考虑模拟;
·因为数据范围很小,我们可以直接用一个二维 char 数组来表示铺面;
·每输入一个音符直接在 数组对应位置修改为对应音符即可;
·考虑时间点存在两个音符的情况(题目中说的不会有双押以上的情况),我们用一个last数组存每个时间点上次出现音符的轨道,然后,然后每次出现last已经有值时向其连线即可;
for (int i = 1; i <= n; i++)
{
int t, h;
cin >> s >> t >> h;
if (s == "tap")
{
mp[t][h] = 'O';
}
else
{
mp[t][h] = 'X';
}
if (last[t])
{
if (last[t] < h)
{
swap(h, last[t]);
}
for (int j = h + 1; j < last[t]; j++) // 向last连线
{
mp[t][j] = '-';
}
}
last[t] = h;
}
·复杂度分析
·时间复杂度,构建铺面O(n) ,输出铺面O(m), 即为O(n + m);
·空间复杂度, 存铺面O(m);
·完整代码
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2010;
int n, m;
string s;
int last[N];
char mp[N][7];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
memset(mp, ' ', sizeof mp); // 记得填充空格
for (int i = 1; i <= n; i++)
{
int t, h;
cin >> s >> t >> h;
if (s == "tap")
{
mp[t][h] = 'O';
}
else
{
mp[t][h] = 'X';
}
if (last[t])
{
if (last[t] < h)
{
swap(h, last[t]);
}
for (int j = h + 1; j < last[t]; j++)
{
mp[t][j] = '-';
}
}
last[t] = h;
}
for (int i = m; i > 0; i--)
{
for (int j = 0; j <= 8; j++)
{
if (j == 0 || j == 8)
{
cout << '|';
}
else
{
cout << mp[i][j];
}
}
cout << '\n';
}
cout << "+-------+\n";
return 0;
}
·希望对大家有所帮助qwq.

京公网安备 11010502036488号