Atcoder ABC 454 题解
观前提醒,本题解仅针对本人写过的题,题目思路仅供参考
若有新思路欢迎分享
A
题目描述
题解
这道题就是简单的输出\([L,R]\)内有多少个整数,输出\(L-R+1\)即可
时间复杂度是\(O(1)\),应该可以接受()
示例代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int l,r;
cin>>l>>r;
cout<<r-l+1;
}
a,b=map(int,input().split())
print(b-a+1)
B
题目描述
题解
对于两个问题,第一个问题就是找出是否有一种衣服被两个以上的人穿,如果没有输出"Yes",否则输出"No"
第二个问题就是看一下是否有什么衣服没有被人穿过,如果有输出"No",否则输出"Yes"
上述对衣服穿的人数的计数,我习惯利用计数哈希,或者开一个计数数组也行
示例代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<int> a(n+1);
map<int,int> ap;
for(int i=1;i<=n;i++){
cin>>a[i];
ap[a[i]]++;
}
bool found=true;
for(int i=1;i<=m;i++){
if(ap[i]>1){
found=false;
}
}
if(!found){
cout<<"No"<<endl;
}
else{
cout<<"Yes"<<endl;
}
found=true;
for(int i=1;i<=m;i++){
if(ap[i]<1){
found=false;
}
}
if(!found){
cout<<"No"<<endl;
}
else{
cout<<"Yes"<<endl;
}
}
C
题目描述
题解
关于这道题,最初我的思路是从头到尾进行模拟,但是被第三个样例卡住了,所以这个思路踢走
由于题目没有限制某种交换的次数,所以我们可以把这道题目抽象成,有一张有n个点m条边的有向图,从点1出发能够到达多少个点。典型的做法是利用bfs暴力搜索,利用vis的布尔数组进行标记达到剪枝效果
时间复杂度在未进行任何剪枝的情况下是\(O(n)\),不过由于进行了剪枝,所以实际程序运行时间小于预计时间
示例代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> g(n+1);
while(m--){
int p,q;
cin>>p>>q;
g[p].push_back(q);
}
vector<bool> vis(n+1,false);
queue<int> bfs;
bfs.push(1);
vis[1]=true;
while(!bfs.empty()){
int u=bfs.front();
bfs.pop();
for(auto v:g[u]){
if(!vis[v]){
vis[v]=true;
bfs.push(v);
}
}
}
int ans=0;
for(int i=1;i<=n;i++){
if(vis[i]){
ans++;
}
}
cout<<ans<<endl;
}
D
题目描述
题解
要判断字符串\(a\)是否能按照上述规则变为字符串\(b\),那么我们可以去看看按照上述规则,字符串\(a\)和\(b\)能不能变为同一个字符串,跑模拟即可
示例代码
#include<bits/stdc++.h>
using namespace std;
string handle(const string& s){
string t;
for(auto c:s){
t.push_back(c);
if(t.size()>=4&&t.substr(t.size()-4,4)=="(xx)"){
t.erase(end(t)-4,end(t));
t+="xx";
}//string的stl库,我再也不黑stl库了
}
return t;
}
void solve(){
string a,b;
cin>>a>>b;
if(handle(a)==handle(b)){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
int main(){
int t;
cin>>t;
while(t--){
solve();
}
}

浙公网安备 33010602011771号