John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers on Weibo – that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are supposed to help him generate the list of winners.
Input Specification:
Each input file contains one test case. For each case, the first line gives three positive integers M (≤ 1000), N and S, being the total number of forwards, the skip number of winners, and the index of the first winner (the indices start from 1). Then M lines follow, each gives the nickname (a nonempty string of no more than 20 characters, with no white space or return) of a follower who has forwarded John’s post.
Note: it is possible that someone would forward more than once, but no one can win more than once. Hence if the current candidate of a winner has won before, we must skip him/her and consider the next one.
Output Specification:
For each case, print the list of winners in the same order as in the input, each nickname occupies a line. If there is no winner yet, print Keep going… instead.

题目大意:
确定第一个后依次确定后面的获奖者,如果该获奖者以及获过奖,就跳到下一个。
思路:
- 用一个map来记录获奖者名单,值为1,如果之前获过奖,就跳过,到下一个。
- 关于跳过到下一个这里,不能使用纯粹输入时的i来计数,应该还是需要一个量来记录,遇到第一个获奖者就变成0,开始计数,如果到下一个获奖者是已获的,则保持不变,继续遍历,直到找到下一个获奖者。
代码:
#include<iostream>
#include<vector>
using namespace std;
vector<string> v;
bool check(string s){
bool flag=false;
for(int i=0;i<v.size();i++) {
if(v[i]==s) flag=true;
}
return flag;
}
int main(){
int m,n,s;
cin>>m>>n>>s;
int snum=0,nnum=0;
while(m--){
string str;
cin>>str;
snum++;
if(snum==s){
v.push_back(str);
continue;
}
else if(snum>s){
if(nnum+1==n){
if(check(str)) continue;
else{
v.push_back(str);
nnum=0;
}
}
else nnum++;
}
}
if(v.size()==0) printf("Keep going...");
else{
for(int i=0;i<v.size();i++) cout<<v[i]<<endl;
}
return 0;
}

因为不清楚unordered_map、unordered_set的插入顺序和遍历顺序,它返回的顺序和需要得到的顺序刚好相反,所以在最后我选取了vector来实现。
tip:
- unordered_map、unordered_set中不能使用rbegin()和rend(); 但map和set可以使用
约翰在微博上举办抽奖活动,从转发他帖子的每N名粉丝中选出赢家,这份包含所有获奖者昵称的完整名单揭示了他们的幸运时刻。
452

被折叠的 条评论
为什么被折叠?



