8.16_407_课堂总结
T637945 落落的去的宇宙
思路
简单字符串,每个国家分三次读入,每次取读入字符串的首字母即可
正解代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin >> n;
while(n--){
string s, ans;
cin >> s;
ans += s[0];
cin >> s;
ans += s[0];
cin >> s;
ans += s[0];
cout << ans << "\n";
}
return 0;
}
T637925 敲鼓
思路
模拟,把字符串p和s分段存储,然后判断每个小段是否满足条件
正解代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int cn1[200001], cn2[200001];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin >> t;
while(t--){
string s1, s2;
cin >> s1 >> s2;
int c1 = 0, c2 = 0;
for(int i = 0; i < s1.size(); i++){
if(s1[i] != s1[i - 1]){
cn1[++c1]++;
}else{
cn1[c1]++;
}
}
for(int i = 0; i < s2.size(); i++){
if(s2[i] != s2[i - 1]){
cn2[++c2]++;
}else{
cn2[c2]++;
}
}
for(int i = 1; i <= max(c1, c2); i++){
if((cn2[i] + 1) / 2 > cn1[i] || cn2[i] < cn1[i]){
cout << "No\n";
break;
}
if(i == max(c1, c2)){
cout << "Yes\n";
}
}
for(int i = 1; i <= max(c1, c2); i++){
cn1[i] = 0;
cn2[i] = 0;
}
}
return 0;
}
T637889 箭头路径
思路
几乎搜索板子,懒得讲 每次搜索先走一步,再跳一下
正解代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<char, int> mpx, mpy;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int vis[3][200001], n;
char c[3][200001];
void init_mp(){
mpx['>'] = 0;
mpy['>'] = 1;
mpx['<'] = 0;
mpy['<'] = -1;
}
struct Node{
int x, y, step;
};
void bfs(int t){
queue<Node> q;
q.push({1, 1, 0});
vis[1][1] = t;
while(q.size()){
Node f = q.front();
q.pop();
if(f.x == 2 && f.y == n){
cout << "YES\n";
return;
}
for(int i = 0; i < 4; i++){
int tx = f.x + dx[i];
int ty = f.y + dy[i];
if(tx > 0 && ty > 0 && tx <= 2 && ty <= n && vis[tx][ty] != t){
int ox = tx, oy = ty;
tx += mpx[c[ox][oy]];
ty += mpy[c[ox][oy]];
if(tx > 0 && ty > 0 && tx <= 2 && ty <= n && vis[tx][ty] != t){
vis[tx][ty] = t;
q.push({tx, ty, f.step + 1});
}
}
}
}
cout << "NO\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
init_mp();
int t;
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= 2; i++){
for(int j = 1; j <= n; j++){
cin >> c[i][j];
}
}
bfs(t + 1);
}
return 0;
}
T638238 传送门
正解思路
由于使用普通的图搜索会RE(数组开小)/MLE(数组开正常),所以只能另想方法
可以用一个map把每个节点存储时的下标变小,再用一个int数组id来存节点的值
正解代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<int> g[1000001];
bool vis[1000001];
map<int, int> mp;
int id[1000001], m;
int mx = 1;
void bfs(){
queue<int> q;
q.push(mp[1]);
while(q.size()){
int f = q.front();
q.pop();
for(int i = 0; i < g[f].size(); i++){
int tx = g[f][i];
if(!vis[tx]){
q.push(tx);
vis[tx] = true;
mx = max(mx, id[tx]);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin >> n;
m++, mp[1] = 1, id[1] = 1;
for(int i = 1; i <= n; i++){
int x, y;
cin >> x >> y;
if(!mp.count(x)){
m++, mp[x] = m, id[m] = x;
}
if(!mp.count(y)){
m++, mp[y] = m, id[m] = y;
}
g[mp[x]].push_back(mp[y]);
g[mp[y]].push_back(mp[x]);
}
bfs();
cout << mx;
return 0;
}