7.27综合测试总结

· · 个人记录

T637945 落落的去的宇宙

题目描述

落落的去 在平行宇宙捏了一个和地球一模一样的星球,并创建了许多国家。每个国家的全称由三个字符串组成。每个字符串的第一个字母连起来就是该国的缩写名称。

给定国家的全称,请输出缩写名称。

输入格式

第一行包含一个整数 t - 独立测试用例的数量( 1 \leq t \leq 100 )。

接下来的 t 行分别包含三个空格分隔的字符串。每个字符串的长度不超过 10 ,且只包含小写字母。

输出格式

对于每个测试用例,输出由每个单词的第一个字母连接而成的字符串。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        string a, b, c;
        cin >> a >> b >> c;
        cout << a[0] << b[0] << c[0] << endl;
    } 
    return 0;
} 

T637925 敲鼓

题目描述

你面前有两面鼓:一面是左鼓,一面是右鼓。敲击左边的鼓可以记录为 "L",敲击右边的鼓可以记录为 "R"。

统治这个世界的奇异力量是善变的:有时敲击一次,有时敲击两次。因此,敲击左边鼓的声音可能是 "L "或 "LL",敲击右边鼓的声音可能是 "R "或 "RR"。

敲击的顺序记录在字符串 p 中,听到的声音记录在字符串 s 中。给定 p 和 s ,判断字符串 s 是否可能是字符串 p 击打的结果。

例如,如果 p= "LR",那么点击的结果可能是 "LR"、"LRR"、"LLR "和 "LLRR "中的任何一个,但字符串 "LLLR "或 "LRL "则不可能。

输入格式

第一行包含一个整数 t ( 1 \leq t \leq 10^4 ) - 独立测试用例的数量。

每个测试用例的第一行包含仅由字符 "R "和 "L "组成的字符串 p ( 1 \le |p| \le 2 \cdot 10^5 ),其中 |p| 表示字符串 p 的长度。

每个测试用例的第二行包含仅由字符 "R "和 "L "组成的字符串 s ( 1 \le |p| \le |s| \le 2 \cdot 10^5 )。

保证所有测试用例中 |s| 的总和不超过 2\cdot 10^5 。

输出格式

对于每组输入数据,如果 s 可以是听到的声音,则输出 "Yes",否则输出 "No"。在任何情况下都可以输出。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> t;
    while (t--) {
        string p, s;
        cin >> p >> s;
        int len1 = p.size(), len2 = s.size();
        if (len1 > len2) {
            cout << "No\n"; continue;
        }
        bool flag = true;
        for (int i = 0, j = 0; i <= len1  && j <= len2; i++, j++) {
            if ((i == len1 && j != len2) || (i != len1 && j == len2)) {
                cout << "No\n"; flag = false; break; 
            }
            if (p[i] == s[j]) {
                if (p[i] == s[j + 1]) j++;
            }
            if (p[i] != s[j]) {
                cout << "No\n"; flag = false; break;  
            }
        }
        if (flag == true) cout << "Yes\n";
    }
    return 0;
} 
/*
5
R
RR
LRLR
LRLR
LR
LLLR
LLLLLRL
LLLLRRLL
LLRLRLRRL
LLLRLRRLLRRRL
*/

错误原因

用了一个碳基生物看不懂的奇怪方法

正确思路

比如:

p = LLLLLRRRLLLRRR s = RRRRRLLLLLRRRRRLLLLL p$每段的个数用$cnt1$数组表示,$cnt1[i]$表示第$i$段的数量为$cnt1[i]

则s每段的个数用cnt2数组表示,cnt2[i]表示第i段的数量为cnt2[i]

那么

cnt1[] = {5, 3, 3, 3} cnt2[] = {8, 5, 5, 5} $5 < 3 * 2$ 并且 $5 > 3$, 比较下一个; $5 < 3 * 2$ 并且 $5 > 3$, 比较下一个; $5 < 3 * 2$ 并且 $5 > 3$, 比较完了, 说明要输出$Yes$; ## 正确代码 ```cpp #include <bits/stdc++.h> #define int long long using namespace std; int t; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { string p, s; cin >> p >> s; int n = p.size(); int m = s.size(); if (m < n || m > n * 2 || p[0] != s[0]) { cout << "No\n"; continue; } if (p == s) { cout << "Yes\n"; continue; } vector<int> a, b; int cnt = 0; char now = p[0]; for (int i = 0; i < p.size(); i++) { if (p[i] == now) cnt++; else { a.push_back(cnt); cnt = 1; now = p[i]; } } a.push_back(cnt); cnt = 0; now = s[0]; for (int i = 0; i < s.size(); i++) { if (s[i] == now) cnt++; else { b.push_back(cnt); cnt = 1; now = s[i]; } } b.push_back(cnt); if (a.size() != b.size()) { cout << "No\n"; continue; } bool f = true; for (int i = 0; i < b.size(); i++) { // cout << a[i] << " " << b[i] << endl; if (b[i] < a[i] || b[i] > a[i] * 2) { cout << "No\n"; f = false; break; } } if (f) cout << "Yes\n"; } return 0; } ``` # T637889 箭头路径 ## 题目描述 网格由 $2$ 行和 $n$ 列组成。行的编号从上到下为 $1$ 至 $2$ 。列的编号从左到右依次为 $1$ 至 $n$ 。网格中的每个单元格都包含一个箭头,指向左边或右边。没有箭头指向网格外。 有一个机器人从 $(1, 1)$ 单元开始。每隔一秒钟,下面两个动作会相继发生: 1. 首先,机器人向左、向右、向下或向上移动(**不能试图移动到网格外,也不能跳过移动**); 2. 然后,机器人沿着放置在当前单元格中的箭头移动(移动后最终到达的单元格)。 您的任务是判断机器人能否到达 $(2, n)$ 单元格。 ## 输入格式 第一行包含一个整数 $t$ ( $1 \le t \le 10^4$ ) - 测试用例数。 每个测试用例的第一行包含一个整数( $2 \le n \le 2 \cdot 10^5$ )。 第二行包含由 $n$ 个字符组成的字符串 '<' 或 '>' 网格的第一行。 第三行包含一个字符串,该字符串正好由 $n$ 个字符'<' 或 '>'组成--即网格的第二行。 输入的其他限制: - $n$ 为偶数; - 没有指向网格外的箭头; - 所有测试用例中 $n$ 的总和不超过 $2 \cdot 10^5$ 。 ## 输出格式 对于每个测试案例,如果机器人可以到达 $(2, n)$ 单元,则打印 "YES";否则,打印 "NO"。 ## 正确代码 ```cpp #include <bits/stdc++.h> #define int long long using namespace std; int t, n; char a[5][200007]; bool vis[5][200007]; struct node { int x, y, f; }; int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; void bfs() { queue<node> q; q.push({1, 1, 1}); vis[1][1] = true; while (!q.empty()) { node cur = q.front(); q.pop(); if (cur.x == 2 && cur.y == n) { cout << "YES\n"; return; } if (cur.f == 0) { if (a[cur.x][cur.y] == '<') vis[cur.x][cur.y - 1] = true, q.push({cur.x, cur.y - 1, 1}); if (a[cur.x][cur.y] == '>') vis[cur.x][cur.y + 1] = true, q.push({cur.x, cur.y + 1, 1}); } else { for (int i = 0; i < 4; i++) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx <= 2 && nx >= 1 && ny >= 1 && ny <= n && vis[nx][ny] == false) { vis[nx][ny] = true; q.push({nx, ny, 0}); } } } } cout << "NO\n"; return; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 1; i <= 2; i++) for (int j = 1; j <= n; j++) cin >> a[i][j], vis[i][j] = false; bfs(); } return 0; } ``` # T638238 传送门 ## 题目描述 有一座很高的楼,你现在在第一层。 有 $n$ 个传送门,每个传送门连接第 $a_i$ 层与 $b_i$ 层。传送门是双向的。 请你求出你能到达的最高楼层。 ## 输入格式 第一行,一个整数 $n$。 接下来 $n$ 行,每行两个数 $a_i$,$b_i$,表示传送门。 ## 输出格式 输出你能到达的最高楼层。 ## 说明/提示 #### 数据范围 $1 \le n \le 2 \times 10^5 1 \le a_i, b_i \le 10^9

保证 a_i \ne b_i。

对于20%的数据:

1 \le n \le 10 1 \le a_i, b_i \le 10^2

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> g[200007];
int n, ans = 1;
map<int, bool> vis;
void bfs() {
    queue<int> q;
    q.push(1);
    vis[1] = true;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        ans = max(ans, cur);
        for (int i = 0; i < g[cur].size(); i++) {
            if (vis[g[cur][i]] == false) {
                vis[g[cur][i]] = true;
                q.push(g[cur][i]);
            }
        }
    }
    return;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x, y; cin >> x >> y;
        if (x >= 500000000) {
            cout << 1; return 0;
        }
        g[x].push_back(y);
        g[y].push_back(x);
    }
    bfs();
    cout << ans;
    return 0;
}

错误原因

没用离散化

正确思路

代码如下:

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> g[200007];
int n, ans = 1;
map<int, bool> vis;
void dfs(int u) {
    ans = max(ans, u);
    for (auto v : g[u]) {
        if (vis[v]) continue;
        vis[v] = true;
        dfs(v);
    }
    return;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x, y; cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    vis[1] = true; 
    dfs(1);
    cout << ans;
    return 0;
}
for (int i = 1; i <= n; i++) {
    int x, y; cin >> x >> y;
    if (mp.count(x) == 0) mp[x] = idx; id[idx] = x; idx++;
    if (mp.count(y) == 0) mp[y] = idx; id[idx] = y; idx++; 
    g[mp[x]].push_back(mp[y]);
    g[mp[y]].push_back(mp[x]);
}
void dfs(int x) {
    ans = max(ans, id[x]);
    for (auto v : g[x]) {
        if (vis[v] == false) {
            vis[v] = true;
            dfs(v);
        }
    }
}

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> g[2000007];
int n, ans = 1,flag;
map<int, bool> vis;
map<int, int> mp;
int id[2000007];
int idx = 1;
void dfs(int x) {
    ans = max(ans, id[x]);
    for (auto v : g[x])
        if (vis[v] == false) {vis[v] = true; dfs(v);}
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x, y; cin >> x >> y;
        if (x == 1 || y == 1) flag = 1;
        if (mp.count(x) == 0) mp[x] = idx; id[idx] = x; idx++;
        if (mp.count(y) == 0) mp[y] = idx; id[idx] = y; idx++; 
        g[mp[x]].push_back(mp[y]);
        g[mp[y]].push_back(mp[x]);
    }
    vis[1] = true;
    if (!flag) {
        cout << 1;
        return 0;
    }
    dfs(1);
    cout << ans;
    return 0;
}