跳到正文

个人记录

7-27课程解析+总结

xiangrui0305约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月26日
篇幅
约 9 分钟

版本

  1. v1当前

7-27课程解析+总结

题目解析+总结

T637925

给定一个字符串p(∣p∣≤2×105)p(|p| \le2 \times 10^5)。其中,LL等价于LL、LLLL,RR等价于RR、RRRR,那么LRLR等价于LRLR、LRRLRR、LLRLLR、LLRRLLRR。再给定一个字符串s(∣s∣≤2×105)s(|s| \le2 \times 10^5)。请问pp是否等价于ss?

分析

样例:

代码
LLLLLRRRLLLRRR
LLLLLLLLRRRRRLLLLLRRRRR

因为字符串只有LL和RR,所以可以直接按照字符分成四段:

代码
LLLLLRRRLLLRRR => LLLLL RRR LLL RRR
cnt1[4] = {5,3,3,3};

同时,ss也要分成四段。

代码
LLLLLLLLRRRRRLLLLLRRRRR => LLLLLLLL RRRRR LLLLL RRRRR
cnt2[4] = {8,5,5,5};

我们可以使用两个变量,将pp和ss分成四段。

C++
char c = 's';
for(int i=0;i<len1;++i){
    if(q[i] != c) c = q[i],cnt++;
    cnt1[cnt]++;
}
c = 's',cnt = 0;
for(int i=0;i<len2;++i){
    if(s[i] != c) c = s[i],cnt++;
    cnt2[cnt]++;
}

同时,每一段都要判断条件:如果不在[c1,c1×2][c_1,c_1 \times2]范围内,输出No

C++
bool flag = 0;
for(int i=1;i<=cnt;++i){
    if(cnt1[i] > cnt2[i] || cnt1[i]*2 < cnt2[i]){
        cout<<"No\n";
        flag = 1;
        break;
    }
}
代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int cnt1[200005],cnt2[200005];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t;
	cin>>t;
	while(t--){
		string q,s;
		cin>>q>>s;
		int len1 = q.size(),len2 = s.size(),cnt = 0;
		memset(cnt1,0,sizeof cnt1);
		memset(cnt2,0,sizeof cnt2);
		char c = 's';
		for(int i=0;i<len1;++i){
			if(q[i] != c) c = q[i],cnt++;
			cnt1[cnt]++;
		}
		c = 's',cnt = 0;
		for(int i=0;i<len2;++i){
			if(s[i] != c) c = s[i],cnt++;
			cnt2[cnt]++;
		}
		bool flag = 0;
		for(int i=1;i<=cnt;++i){
			if(cnt1[i] > cnt2[i] || cnt1[i]*2 < cnt2[i]){
				cout<<"No\n";
				flag = 1;
				break;
			}
		}
		if(!flag) cout<<"Yes\n";
	}
	return 0;
}
总结+错误原因

用搜索效率不高,容易超时,建议使用模拟

T637889

网格由22行nn列组成。你从第(1,1)(1,1)个格子开始先在不移动到网格外的情况下向左、向右、向下或向上移动,然后沿着放置在当前单元格中的箭头移动。请问能否到达第(2,n)(2,n)个格子?

代码+注释
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
char a[3][200005];
bool vis[3][200005];
struct node{
	int x,y;
};
bool check(int x1,int y1){ //判断是否越界和是否走过
	return x1 >= 1 && x1 <= 2 && y1 >= 1 && y1 <= n && !vis[x1][y1];
}
void BFS(int x1,int y1){
	queue<node>op;
	op.push({x1,y1});
	vis[x1][y1] = 1;
	while(op.size()){
		node top = op.front();
		op.pop();
		if(top.x == 2 && top.y == n){
			cout<<"YES\n";
			return ;
		}
		int dx[] = {-1,1,0,0};
		int dy[] = {0,0,-1,1};
    //遍历的四个方向
		for(int i=0;i<4;++i){
			int xx = dx[i] + top.x;
			int yy = dy[i] + top.y;
			if(check(xx,yy)){
				vis[xx][yy] = 1;
				if(a[xx][yy] == '<' && check(xx,yy-1)) yy=yy-1,vis[xx][yy] = 1,op.push({xx,yy});
				else if(a[xx][yy] == '>' && check(xx,yy+1)) yy=yy+1,vis[xx][yy] = 1,op.push({xx,yy});
        //这里不仅要标记上下左右四个方向的位置(主动),还要标记一下根据左右箭头移动后的位置(被动),并入队。
			}
		}
	}
	cout<<"NO\n";
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t;
	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] = 0; //复原标记的位置
			}
		}
		BFS(1,1);
	}
	return 0;
}
总结

使用搜索

T638238

给定一个无向图,图中aia_i连接bib_i。你在第一个点,请你求出你能到达的最大编号的点。

分析

因为1≤ai,bi≤1091 \le a_i,b_i \le 10^9,所以这样开一定会爆栈。

C++
vector<int>mp[1000000005];

那要怎么办呢?如何要保证不爆栈呢? 这时,我们可以把这些数用编号保存起来,同时要反过来存一次,这样可以用这些编号建图了。 这就是离散化的初步原理。通俗来讲,就是当有些数据因为本身很大或者类型不支持,自身无法作为数组的下标来方便地处理,而影响最终结果的只有元素之间的相对大小关系时,我们可以将原来的数据按照排名来处理问题。这就叫离散化。 我们建完图后,还要把11也要取个编号。

C++
for(int i=1;i<=n;++i){
    int a,b;
    cin>>a>>b;
    if(mp.count(a) == 0) mp[a] = ++fub,id[fub] = a;
    if(mp.count(b) == 0) mp[b] = ++fub,id[fub] = b;
    op[mp[a]].push_back(mp[b]);
    op[mp[b]].push_back(mp[a]);
}
vis[mp[1]] = 1;

随后,我们使用一个变量将去过的最大编号存起来(用原来的)。接着使用迭代器,遍历整个图。

C++
num = max(num,id[cur]);
for(auto v:op[cur]){
    if(vis[v]) continue;
    vis[v] = 1;
    DFS(v);
}
代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,fub = 0,num = 1;
vector<int>op[2000005];
map<int,int>mp;
map<int,int>id;
map<int,int>vis;
void DFS(int cur){
	num = max(num,id[cur]);
	for(auto v:op[cur]){
		if(vis[v]) continue;
		vis[v] = 1;
		DFS(v);
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i){
		int a,b;
		cin>>a>>b;
		if(mp.count(a) == 0) mp[a] = ++fub,id[fub] = a;
		if(mp.count(b) == 0) mp[b] = ++fub,id[fub] = b;
		op[mp[a]].push_back(mp[b]);
		op[mp[b]].push_back(mp[a]);
	}
	vis[mp[1]] = 0;
	DFS(mp[1]);
	cout<<num<<endl;
	return 0;
}
总结+错误原因

我通过直接建图那部分分,但是没有拿到。后来才发现需要使用离散化和迭代器写

原文