跳到正文

个人记录

7.27综合测试总结

#SUSPENDED#约 18 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月21日
篇幅
约 18 分钟

版本

  1. v1当前

T637945 落落的去的宇宙

题目描述

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

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

输入格式

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

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

输出格式

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

正确代码

C++
#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 敲鼓

题目描述

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

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

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

例如,如果 p=p= "LRLR",那么点击的结果可能是 "LRLR"、"LRRLRR"、”LLRLLR “和 ”LLRRLLRR “中的任何一个,但字符串 ”LLLRLLLR “或 ”LRLLRL “则不可能。

输入格式

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

每个测试用例的第一行包含仅由字符 ”RR “和 ”LL “组成的字符串 pp ( 1≤∣p∣≤2⋅1051 \le |p| \le 2 \cdot 10^5 ),其中 ∣p∣|p| 表示字符串 pp 的长度。

每个测试用例的第二行包含仅由字符 ”RR “和 ”LL “组成的字符串 ss ( 1≤∣p∣≤∣s∣≤2⋅1051 \le |p| \le |s| \le 2 \cdot 10^5 )。

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

输出格式

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

错误代码

C++
#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=Lp = L, ss可能是:LL, LLLL

    如果p=LLp = LL, ss可能是:LLLL, LLLLLL, LLLLLLLL

    如果p=LLLp = LLL, ss可能是:LLLLLL, LLLLLLLL,LLLLLLLLLL, LLLLLLLLLLLL

    如果p=LLLLp = LLLL, ss可能是:LLLLLLLL, LLLLLLLLLL,LLLLLLLLLLLL, LLLLLLLLLLLLLL, LLLLLLLLLLLLLLLL

  • 我们可以发现:如果LL的数量为cntcnt个, 可以变动的范围是 [cnt,cnt∗2][cnt, cnt * 2]

  • 面对pp和ss两个字符串, 我们都可以把每段连续的字母的个数记录起来

  • 记录起来后, 我们可以先比一下pp和ss的段数, 如果不一样就肯定要输出NoNo, 否则就把pp和ss的每一段一一比较, pp每段连续的字母数量记为cnt1cnt1, ss每段连续的字母数量记为cnt2cnt2, 如果cnt2<cnt1cnt2 < cnt1或cnt2>cnt1∗2cnt2 > cnt1 * 2, 就要输出NoNo, 否则继续比较

比如:

p=LLLLLRRRLLLRRRp = LLLLLRRRLLLRRR

s=RRRRRLLLLLRRRRRLLLLLs = RRRRRLLLLLRRRRRLLLLL

pp每段的个数用cnt1cnt1数组表示,cnt1[i]cnt1[i]表示第ii段的数量为cnt1[i]cnt1[i]

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

那么

cnt1[]=5,3,3,3cnt1[] = {5, 3, 3, 3}

cnt2[]=8,5,5,5cnt2[] = {8, 5, 5, 5}

8<5∗28 < 5 * 2 并且 8>58 > 5, 比较下一个;

5<3∗25 < 3 * 2 并且 5>35 > 3, 比较下一个;

5<3∗25 < 3 * 2 并且 5>35 > 3, 比较下一个;

5<3∗25 < 3 * 2 并且 5>35 > 3, 比较完了, 说明要输出YesYes;

正确代码

C++
#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 箭头路径

题目描述

网格由 22 行和 nn 列组成。行的编号从上到下为 11 至 22 。列的编号从左到右依次为 11 至 nn 。网格中的每个单元格都包含一个箭头,指向左边或右边。没有箭头指向网格外。

有一个机器人从 (1,1)(1, 1) 单元开始。每隔一秒钟,下面两个动作会相继发生:

  1. 首先,机器人向左、向右、向下或向上移动(不能试图移动到网格外,也不能跳过移动);
  2. 然后,机器人沿着放置在当前单元格中的箭头移动(移动后最终到达的单元格)。

您的任务是判断机器人能否到达 (2,n)(2, n) 单元格。

输入格式

第一行包含一个整数 tt ( 1≤t≤1041 \le t \le 10^4 ) - 测试用例数。

每个测试用例的第一行包含一个整数( 2≤n≤2⋅1052 \le n \le 2 \cdot 10^5 )。

第二行包含由 nn 个字符组成的字符串 ’<’ 或 ’>’ 网格的第一行。

第三行包含一个字符串,该字符串正好由 nn 个字符’<’ 或 ’>‘组成—即网格的第二行。

输入的其他限制:

  • nn 为偶数;
  • 没有指向网格外的箭头;
  • 所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5 。

输出格式

对于每个测试案例,如果机器人可以到达 (2,n)(2, n) 单元,则打印 “YES”;否则,打印 “NO”。

正确代码

C++
#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 传送门

题目描述

有一座很高的楼,你现在在第一层。

有 nn 个传送门,每个传送门连接第 aia_i 层与 bib_i 层。传送门是双向的。

请你求出你能到达的最高楼层。

输入格式

第一行,一个整数 nn。

接下来 nn 行,每行两个数 aia_i,bib_i,表示传送门。

输出格式

输出你能到达的最高楼层。

说明/提示

数据范围

1≤n≤2×1051 \le n \le 2 \times 10^5

1≤ai,bi≤1091 \le a_i, b_i \le 10^9

保证 ai≠bia_i \ne b_i。

对于20%的数据: 1≤n≤101 \le n \le 10

1≤ai,bi≤1021 \le a_i, b_i \le 10^2

错误代码

C++
#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;
}

错误原因

没用离散化

正确思路

  • 如果不看数据范围这就是一个很简单的题目

代码如下:

C++
#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;
}
  • 但数据太大, 显然vectorvector开不了这么大;

    可是仔细想想就会想到实际上不需要这么多, 最大2e52e5条边;

    我们可以给每个顶点一个编号, 用这个编号建图;

    可以用一个mapmap来存储每个顶点的编号, 还可以用一个idid数组来存储每个编号对应的顶点

    只需要在输入是按照一下代码来即可

C++
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]);
}
  • dfsdfs内代码如下:
C++
void dfs(int x) {
	ans = max(ans, id[x]);
	for (auto v : g[x]) {
		if (vis[v] == false) {
			vis[v] = true;
			dfs(v);
		}
	}
}

正确代码

C++
#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;
}
原文