【题解】Atcoder Beginner Contest 454(ABC454) A~D
A - Closed interval
直接计算 \(r-l+1\)。
#include<bits/stdc++.h>
using namespace std;
int l,r;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>l>>r;
cout<<r-l+1;
return 0;
}
B - Mapping
用一个桶模拟。
#include<bits/stdc++.h>
using namespace std;
int n,m;
int t[110];
bool flag1,flag2;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
int x;
cin>>x;
if(t[x]) flag1=1;
t[x]++;
}
if(flag1) cout<<"No\n";
else cout<<"Yes\n";
for(int i=1;i<=m;i++){
if(!t[i]){
flag2=1;
break;
}
}
if(flag2) cout<<"No\n";
else cout<<"Yes\n";
return 0;
}
C - Straw Millionaire
用 \(A_i\) 交换物品 \(B_i\) 相当于走一条 \(A_i\rightarrow B_i\) 的有向边。这样把图建出来跑一次 DFS,统计一下能走到多少节点即可。
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int n,m,ans;
int h[N],tot;
bool vis[N];
struct Node{
int to,nxt;
}e[N];
void Add(int u,int v){
tot++;
e[tot].to=v;
e[tot].nxt=h[u];
h[u]=tot;
}
void dfs(int u){
vis[u]=1;
for(int i=h[u];i;i=e[i].nxt){
int v=e[i].to;
if(vis[v]) continue;
dfs(v);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
Add(u,v);
}
vis[1]=1;
dfs(1);
for(int i=1;i<=n;i++) ans+=vis[i];
cout<<ans;
return 0;
}
D - (xx)
注意到替换是双向的,而要去替换为带括号的 (xx),需要多少个括号不好处理。所以把两个串能替换掉的括号都替换掉,这时候两个串都不能再向 xx 替换了,只能往外边套括号,套上去的也可以删所以没什么用,比较一下两个串是否相等就可以了。
实现的话可以用一个栈,从前往后往栈里加东西,每次往栈里加东西看一下栈顶是不是 (xx),是的话就替换为 xx,最后从栈底往栈顶输出就可以了。
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+10;
int T;
int n,m;
string a,b;
string s,t;
char st[N],top;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>T;
while(T--){
cin>>a>>b;
n=a.size(),m=b.size();
a=" "+a,b=" "+b;
s=" ",t=" ";
top=0;
for(int i=1;i<=n;i++){
st[++top]=a[i];
if(top>=4){
if(st[top-3]=='('&&st[top-2]=='x'&&st[top-1]=='x'&&st[top]==')'){
top-=4;
st[++top]='x';
st[++top]='x';
}
}
}
for(int i=1;i<=top;i++) s+=st[i];
top=0;
for(int i=1;i<=m;i++){
st[++top]=b[i];
if(top>=4){
if(st[top-3]=='('&&st[top-2]=='x'&&st[top-1]=='x'&&st[top]==')'){
top-=4;
st[++top]='x';
st[++top]='x';
}
}
}
for(int i=1;i<=top;i++) t+=st[i];
if(s==t) cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}

浙公网安备 33010602011771号