2025.07.27课堂总结
闲话
今天双考试了。我觉得有点小可惜啊~
T637925 敲鼓
这道题我的主要问题是:判断是否可能时判断错误,且在理解题意方面可能?存在一点理解性问题,错误石山代码如下:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int k[N + 10][2];
string p, s, fl, fr;
void fun(){
for(int i = 1; i <= N; i++){
k[i][2] = i;
k[i][1] = (i % 2 == 0 ? i / 2 : (i / 2) + 1); //wrong
}
}
int main(){
fun();
int t;
cin >> t;
while(t--){
queue<int> l, r;
cin >> p >> s;
p.push_back('K');
s.push_back('K');
int cnt = 0, su = 0;
char st = p[0];
while(cnt < p.size()){
if(p[cnt] == st){
su++;
}else{
fl.push_back(st);
st = p[cnt];
l.push(su);
su = 1;
}
cnt++;
}
cnt = 0, su = 0;
st = s[0];
while(cnt < s.size()){
if(s[cnt] == st){
su++;
}else{
fr.push_back(st);
st = s[cnt];
r.push(su);
su = 1;
}
cnt++;
}
bool f = 0;
cnt = 0;
int minn = min(l.size(), r.size());
//
while(cnt < minn){
if(fl[cnt] != fr[cnt] || k[r.front()][1] > r.front() || k[r.front()][2] < r.front() || k[l.front()][1] > l.front() || k[l.front()][2] < l.front()){//wrong
f = 1;
}
cnt++;
}
//if(!l.empty() || !r.empty()) f = 1;
if(f == 1){
cout << "No"<<endl;
}else cout << "Yes"<<endl;
f = 0;
}
return 0;
}我在补题时察觉到了上述的错误,并进行了修改:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int k[N + 10][2];
string p, s;
void fun(){//打表记录i个L或R情况下可选最小量和最大量
for(int i = 1; i <= N; i++){
k[i][1] = i;
k[i][2] = 2 * i;
}
}
int main(){
fun();
int t;
cin >> t;
while(t--){
string fl, fr;
queue<int> l, r;
cin >> p >> s;
p.push_back('K');//push防没记录最后一个字段
s.push_back('K');//同上
int cnt = 0, su = 0;
char st = p[0];
while(cnt < p.size()){//记录相同内容的字段,L,R
if(p[cnt] == st){
su++;
}else{
fl.push_back(st);
st = p[cnt];
l.push(su);//记录字段类型为L还是R
su = 1;
}
cnt++;
}
cnt = 0, su = 0;//统计前初始化
st = s[0];
while(cnt < s.size()){//记录相同内容的字段,L,R
if(s[cnt] == st){
su++;
}else{
fr.push_back(st);
st = s[cnt];
r.push(su);//记录字段类型为L还是R
su = 1;
}
cnt++;
}
bool f = 0;
cnt = 0;
int minn = min(l.size(), r.size());
//
while(cnt < minn){
if(fl[cnt] != fr[cnt] || !(k[l.front()][1] <= r.front() && k[l.front()][2] >= r.front())){//修改后
f = 1;
break;
}
l.pop();//更新头(删)
r.pop();//更新头(删)
cnt++;
}
if(f == 1){//输出
cout << "No"<<endl;
}else cout << "Yes"<<endl;
f = 0;
}
return 0;
}T637889 箭头路径
这题因为时间问题所以我没交代码骗分,但说实话是一道模板dfs题,依题意照着写即可。代码如下:
#include<bits/stdc++.h>
using namespace std;
string s[15];
int vis[10005][10005],n,f;
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
bool check(int x,int y){
return(x >= 1 && x <= 2 && y >= 1 && y <= n && vis[x][y] == 0);//判断条件
}
void dfs(int x,int y){
if(x == 2 && y == n){
f = 1;
return;
}
for(int i = 0;i < 4; i++){
int xx = x + fx[i];
int yy = y + fy[i];
if(check(xx,yy)){
vis[xx][yy] = 1;
if(s[xx][yy-1]=='>'){
vis[xx][yy + 1] = 1;
dfs(xx, yy + 1);
}
else if(s[xx][yy - 1] == '<'){
vis[xx][yy - 1] = 1;
dfs(xx, yy - 1);
}
}
}
}
int main(){
int T;
cin>>T;
while(T--)
{
cin >> n;
cin >> s[1] >> s[2];
f = 0;
dfs(1,1);
for(int i = 1; i <= 2; i++)
{
for(int j = 0; j <= n; j++)
vis[i][j] = 0;
}
if(f == 1) cout<<"YES"<<endl;//输出答案
else cout<<"NO"<<endl;
}
return 0;
} T638238 传送门
这道题毕竟是个黄题难度,要脑子和时间。难的在于明确解决方法和代码实现,代码如下:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, maxx;
map<int, vector<int> > mp;
map<int, int> flag;//图的标记
void bfs(int x){//搜索
flag[x] = 1;
maxx = max(maxx, x);
for(int i = 0; i < mp[x].size(); i++){
if(flag[mp[x][i]]) continue;
bfs(mp[x][i]);
}
return ;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
int x, y;
cin >> x >> y;
mp[x].push_back(y);//建图
mp[y].push_back(x);
}
bfs(1);
cout<< maxx;//输出答案
return 0;
}