言归正传,开始保姆级教学!
一、读题
这道题有个优点,只需要阅读输入格式和输出格式就能看懂了,题目背景和题目描述一点用也没有。 题意也不需要概括了,简洁明了。
二、分析
你是不是第一眼就想到了模拟?没问题,但我提出一种不完全是模拟的算法。 其实很简单,不过需要你会用 map,因为 map 可以用 string 作为下标。 先读入本月工作的志愿者,并标记为下个月继续工作。再读入下个月不工作的志愿者,并标记。然后遍历本月工作的志愿者,下个月继续工作就记录到数组中。接着读入新来的志愿者,也记录到数组中。最后做一个简简单单的 sort 排序就完成啦! 是不是很简单呢?
三、代码实现
准备工作:
int n,m,l,tot=0;//tot记录下个月工作的人数
string people[200010];//记录本月的题目管理志愿者
map<string,bool> still_work;//记录本月志愿者是否继续工作
string work[200010];//记录下个月工作的人当然还有:
#include<bits/stdc++.h>
using namespace std;或者:
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;主体函数:
int main(){
cin>>n>>m>>l;
for(int i=1;i<=n;i++){
cin>>people[i];
still_work[people[i]]=true;
}
for(int i=1;i<=m;i++){
string s;
cin>>s;
still_work[s]=false;
}
for(int i=1;i<=n;i++){
if(still_work[people[i]])work[++tot]=people[i];
}
for(int i=1;i<=l;i++){
string s;
cin>>s;
work[++tot]=s;
}
sort(work+1,work+tot+1);
for(int i=1;i<=tot;i++){
cout<<work[i]<<endl;
}
return 0;
}简简单单,大功告成!