题目链接:
https://pintia.cn/problem-sets/994805260223102976/problems/994805265159798784
AC代码:
#include <bits/stdc++.h>
using namespace std;
vector<string> list_;
vector<string> lottery;
int main(){
int M,N,S;
scanf("%d%d%d",&M,&N,&S);
getchar();
int m=M;
while(m--){
string str;
cin>>str;
getchar();
list_.push_back(str);
}
if(M<S){
printf("Keep going...");
return 0;
}
for(int i=S-1;i<M;i+=N){
if(i==S-1){
lottery.push_back(list_[i]);
}
else{
while(find(lottery.begin(),lottery.end(),list_[i])!=lottery.end()){
//找到了
i++;
}
lottery.push_back(list_[i]);
}
}
for(int i=0;i<lottery.size();i++){
cout<<lottery[i]<<endl;
}
}
二刷:(15分代码,测试点3过不了...)
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> zhong;
string peo[1010];
int main(){
int m,n,s,i,j;
cin>>m>>n>>s;
getchar();
for(i=1;i<=m;i++){
cin>>peo[i];
getchar();
}
if(s>m){
printf("Keep going...");
}
else{
zhong.push_back(peo[s]);
i=s+n;
while(i<=m){
if(find(zhong.begin(),zhong.end(),peo[i])!=zhong.end()){
i++;
}
zhong.push_back(peo[i]);
i+=n;
}
}
int len=zhong.size();
for(j=0;j<len;j++){
cout<<zhong[j]<<endl;
}
return 0;
}
二刷(用map):
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
map<string, int> mp;
string peo[1010];
int main() {
int m, n, s, i, j;
cin >> m >> n >> s;
getchar();
for (i = 1; i <= m; i++) {
cin >> peo[i];
getchar();
}
if (s > m) {
printf("Keep going...");
}
else {
mp[peo[s]] = 1;
cout << peo[s] << endl;
i = s + n;
while (i <= m) {
if (mp[peo[i]] == 0) {
mp[peo[i]] = 1;
cout << peo[i] << endl;
i = i + n;
}
else {
i++;
}
}
}
return 0;
}