T637925 敲鼓
分析:每串相同字母为一段,再统计每段原声音和听到的声音的相同字母数量,只要听到的声音的字母数量<=原声字母数量*2且>=原声字母数即可输出Yes
#include<bits/stdc++.h>
using namespace std;
int t;
int a[200005],a1[200005];
int b[200005],b1[200005];
int main(){
cin >> t;
while(t--){
string s,p;
bool f = 0;
cin >> s >> p;
int n = s.size(),w = p.size();
for(int i = 1;i <= w;i++){
a[i] = 0,a1[i] = 0,b[i] = 0,b1[i] = 0;
}
if(p == s){
cout << "Yes" << '\n';
continue;
}
int sum = 0,sum1 = 0;
int j = 1,k = 1;
for(int i = 0;i < n;i++){
if(s[i] == 'L'){//统计原声每段的L的字母数量
a[j]++;
}
else{//统计原声每段的R的字母数量
b[j]++;
}
if(s[i] == 'L' and s[i + 1] == 'R' or (s[i] == 'R' and s[i + 1] == 'L')) j++;//段数++
//cout << a[i] << ' ';
}
int ans = 0,ans1 = 0,aa = 1,bb = 1;
for(int i = 0;i < w;i++){
if(p[i] == 'L'){
a1[aa]++;//统计听到声音声每段的L的字母数量
}else{
b1[aa]++;//统计听到声音声每段的R的字母数量
}
//cout << a1[aa] << ' ' << a[aa] << ' ';
if(p[i] == 'L' and p[i + 1] == 'R' or p[i] == 'R' and p[i + 1] == 'L') aa++;//段数++
}
for(int i = 1;i <= j;i++){
//cout << a1[i] << ' ' <<a[i] << ' ';
if(a1[i] > a[i] * 2 or a1[i] < a[i]){
//cout << j << ' ';
f = 1;//不符合条件
break;
}
if(b1[i] > b[i] * 2 or b1[i] < b[i]){
//cout << b1[i] << ' ';
f = 1;
break;
}
}
if(f == 0) cout << "Yes" << '\n';
else cout << "No" << '\n';
}
return 0;
}
T637889 箭头路径
分析:广搜的板子,如果遇到箭头特判向左向右
#include<bits/stdc++.h>
using namespace std;
int t,n;
bool vis[3][200005];
char d[3][200005];
int dir[10][10] = {{1,0},{0,1},{-1,0},{0,-1}};//方向数组
struct node{
int x,y;
};
void bfs(){
queue<node> q;
q.push({1,1});
vis[1][1] = 1;//标记起点
while(q.size()){
node f = q.front();//取得队首
q.pop();
if(f.x == 2 and f.y == n){
cout << "YES" << '\n';
vis[f.x][f.y] == 1;
return ;
}
for(int i = 0;i < 4;i++){
int xx = f.x + dir[i][0];//上下左右
int yy = f.y + dir[i][1];
if(xx >= 1 and xx <= 2 and yy >= 1 and yy <= n and vis[xx][yy]!=1){
vis[xx][yy] = 1;
if(d[xx][yy] == '>'){//如果遇到>向右走
vis[xx][yy + 1] = 1;
q.push({xx,yy + 1});
}else{//遇到<向左走
vis[xx][yy - 1] = 1;
q.push({xx,yy - 1});
}
}
}
}
cout << "NO" << '\n';
}
int main(){
cin >> t;
while(t--){
cin >> n;
memset(vis,0,sizeof(vis));
for(int i = 1;i <= 2;i++){
for(int j = 1;j <= n;j++){
cin >> d[i][j];// 输入箭头
}
}
bfs();//开搜
}
return 0;
}
T638238 传送门
分析:思路不难,建图搜索即可,难点在于数据达到了1e9,会报栈,所有要给数据离散化,给每个顶点一个编号,用编号建图
#include<bits/stdc++.h>
using namespace std;
int n,max1 = 0;
map<int,int> vis;
map<int,vector<int> > a;
int idx;
void dfs(int u){
vis[u] = 1;
max1 = max(u,max1);
for(int i = 0;i < a[u].size();i++){
int v = a[u][i];
if(vis[v] == 1) continue;
//vis[v] = 1;
dfs(v);
}
return ;
}
int main(){
cin >> n;
bool f = 0;
for(int i = 1;i <= n;i++){
long long x,y;
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
cout << max1;
return 0;
}