-
题目见这里
-
用链表保存Followers,每个插入链表的对象对应一个flag(初值为false),必须输出对象后才能置flag为true,注意每次输出必须保证tmp
<
s(tmp为有效对象个数) -
代码如下:
#include <iostream>
#include <string>
#include <list>
using namespace std;
const int M = 1005;
int main(){
// freopen("Data.txt","r",stdin);
list<string> forwards;
bool flag[M]={false}; //某字符串是否输出
string forward;
int m,n,s,tmp,index,i;
cin >> m >> n >> s;
tmp = 0;
for(i=1;i<=m;i++){
cin >> forward;
if(m<s) continue; //no winners
index = 0;
list<string>::iterator iter;
for(iter=forwards.begin();iter!=forwards.end();iter++){
index ++;
if(*iter==forward) break;
}
if(!hash[index] || iter==forwards.end()){
if(iter==forwards.end()){
forwards.push_back(forward); //尾部插入
index ++;
}
tmp ++;
if(tmp<s) continue; //反例:11 2 4(输出第二个,不加判断的话)
if(!((tmp-s)%n)){
cout << forward << '\n';
flag[index] = true;
}
}
}
if(m<s) cout << "Keep going..." << '\n';
return 0;
}