T637945 落落的去的宇宙
解题思路
这道就纯字符串题,只需按题意输入,然后去每个字符串开头的第一个字符,拼出一个字符串即可,并输出拼出的字符串
参考代码
#include <bits/stdc++.h>
using namespace std;
int t;
string s, s2;
int main()
{
cin >> t;
while (t --)
{
s2 = "";
for (int i = 1; i <= 3; i ++) cin >> s, s2 += s[0];
cout << s2 << '\n';
}
return 0;
}T637925 敲鼓
正确思路分析
这道题我们可以根据题目发现顺序必须一致和每一段 L 和 R 出现的次数,那么我们就可以用分段计数法,它其实就是平台问题的演变,如果当前是连续相同的字符,那么 ++,否则,将 压入数组, 和 进行一遍,最后,就是比较 和 了( 用来记录 , 用来记录 ),接下来,还要判断 ,为什么呢?因为,如果大于了,那么说明少敲了,或者说,少了一段,这是不可能的;接着循环,范围是 ,其实 ,也可以,因为顺序是相同的;里面还需判断 和 ,为什么呢,因为若是 ,那么说明 中敲的比 敲的还少,若是 ,说明 多敲了,并不能与 一一对应,不符合题目意思,输出 ,打上标记,最后,若没有标记,则输出
参考代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int t;
string p, s;
vector<int> a, b;
int main()
{
cin >> t;
while (t --)
{
bool f = 1;
cin >> p >> s;
int cnt = 1;
a.clear();
b.clear();
for (int i = 1; i < p.size(); i ++)
{
if (p[i] == p[i - 1]) cnt ++;
else a.push_back(cnt), cnt = 1;
}
a.push_back(cnt);
cnt = 1;
for (int i = 1; i < s.size(); i ++)
{
if (s[i] == s[i - 1]) cnt ++;
else b.push_back(cnt), cnt = 1;
}
b.push_back(cnt);
if (a.size() > b.size()) {f = 0; cout << "No\n"; continue;}
for (int i = 0; i < a.size(); i ++)
{
if (a[i] > b[i] || a[i] * 2 < b[i]) {f = 0; cout << "No\n"; break;}
}
if (f) cout << "Yes\n";
}
return 0;
}T637889 箭头路径
思路分析
这道题就是道 ➕ 模拟的题,不过这里的 我们需要在加上一些变量,一个是 ,用来执行动作,一个是 ,用来决定运动的方向。第一个动作就是 ,不过入队时,要让 等于 ,说明我们将执行下一步,还有 是 a[nx][ny],可 a[nx][ny] 是字符,要想使用 {…} (入队操作),那么就需要在输入时,进行操作,这里我是判断字符如果为 ,那么 a[i][j] 设为 ,否则设为 。如果是第二个动作(t.f == 1),就需要判断运动方向(),以及是否越界,这里入队时,记得将 设为 ,以便下一秒的操作,最后判断如果到达终点,打上标记(因为我的 函数是 类型的),最后看有没有标记,并输出即可
参考代码
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 2e5 + 5;
struct node
{
int x, y, f, fw;
};
int t, n, dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0}, a[5][N];
char x;
bool ff, vis[5][N];
void bfs(int x, int y)
{
queue<node> q;
q.push({1, 1, 0, -1});
vis[1][1] = 1;
while (!q.empty())
{
node t = q.front();
q.pop();
if (t.x == 2 && t.y == n) {ff = 1; return;}
if (!t.f)
{
for (int i = 0; i < 4; i ++)
{
int nx = t.x + dx[i], ny = t.y + dy[i];
if (nx < 1 || nx > 2 || ny < 1 || ny > n || vis[nx][ny]) continue;
q.push({nx, ny, 1, a[nx][ny]});
vis[nx][ny] = 1;
}
}
else
{
if (t.fw == 0 && !vis[t.x][t.y + 1] && t.y > 0 && t.y + 1 <= n) q.push({t.x, t.y + 1, 0, -1}), vis[t.x][t.y + 1] = 1;
else if (!vis[t.x][t.y - 1] && t.y > 0 && t.y - 1 <= n) q.push({t.x, t.y - 1, 0, -1}), vis[t.x][t.y - 1] = 1;
}
}
return;
}
int main()
{
cin >> t;
while (t --)
{
ff = 0;
memset(vis, 0, sizeof(vis));
cin >> n;
for (int i = 1; i <= 2; i ++)
{
for (int j = 1; j <= n; j ++)
{
cin >> x;
if (x == '>') a[i][j] = 0;
else a[i][j] = 1;
}
}
bfs(1, 1);
if (ff) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}T638238 传送门
正确解题思路
这道题基本就是图的搜索,然后找最大深度,这里主要讲解一下,如何优化
这里数据有 ,你总不能开空间,开到 吧,不过, 可以轻松帮我们办到,我们只需要将这些巨大无比的数用一个个编号代替,并进入后面的步骤即可,可是我们最好要输出的是最大高度,总不能输出编号吧,所以我们还需要个数组,来解编号,不过因为会有重边,所以要判断是否出现过,没出现过,才能标编号和存编号的真实数值,不过请注意我们要用编号,所以存边时存的是编号,也就是 G[mp[a]].push_back(mp[b]),不过还有一点,就是万一它并没有给我们第一层,那该怎么办,于是要初始化一下 和 , 要用节点 也要是编号, 里存最大深度(最高楼层)就得改为 了,因为 只是编号,所以要用 来解出真实数据,最后输出即可
参考代码
#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int N = 6e5 + 5;
int n, maxn, id[N], pos, a, b;
bool f[N];
vector<long long> G[N];
map<long long, int> mp;
void dfs(int u)
{
maxn = max(maxn, id[u]);
for (int i = 0; i < G[u].size(); i ++)
{
int v = G[u][i];
if (f[v]) continue;
f[v] = 1;
dfs(v);
}
}
int main()
{
cin >> n;
pos ++, mp[pos] = pos, id[1] = pos;
for (int i = 1; i <= n; i ++)
{
cin >> a >> b;
if (!mp.count(a)) pos ++, mp[a] = pos, id[pos] = a;
if (!mp.count(b)) pos ++, mp[b] = pos, id[pos] = b;
G[mp[a]].push_back(mp[b]), G[mp[b]].push_back(mp[a]);
}
dfs(mp[1]);
cout << maxn;
return 0;
}