记录编号 |
84878 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[USACO Nov]FJ没有大的棕色的牛 |
最终得分 |
100 |
用户昵称 |
cstdio |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.014 s |
提交时间 |
2013-12-21 13:28:43 |
内存使用 |
0.32 MiB |
显示代码纯文本
/*
ID:cstdio
PROG:nocow
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<cstring>
#include<deque>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
const int SIZEN=101;
const int SIZEADJ=31;
set<string> adjlist[SIZEADJ];
vector<string> describe[SIZEN];
int gsnum[SIZEADJ]={0};//空串的深度为0
int N,K;
int adjnum;
vector<string> ans;
class NODE{
public:
map<string,int> c;
string data;
int deep;
int num;
NODE(){
deep=0;
c.clear();
data.clear();
num=0;
}
void decnum(int,int);
void getans(int);
int getgs(string);
};
vector<NODE> tree;
#define root tree[0]
void NODE::decnum(int x,int k){
num++;
if(k==adjnum) return;
map<string,int>::iterator key;
key=c.find(describe[x][k]);
if(key!=c.end()) tree[key->second].decnum(x,k+1);
else{
c[describe[x][k]]=tree.size();
NODE temp;
temp.data=describe[x][k];
temp.deep=deep+1;
tree.push_back(temp);
tree.back().decnum(x,k+1);
}
}
int NODE::getgs(string st){
map<string,int>::iterator key;
key=c.find(st);
if(key==c.end()) return gsnum[deep+1];
else return gsnum[deep+1]-tree[key->second].num;
}
void NODE::getans(int leftnum){
int sum=0;
if(deep) ans.push_back(data);
if(deep==adjnum) return;
set<string>::iterator key;
map<string,int>::iterator son;
key=adjlist[deep+1].begin();
while(sum+getgs(*key)<leftnum){
sum+=getgs(*key);
key++;
}
leftnum-=sum;
son=c.find(*key);
if(son==c.end()){
c[*key]=tree.size();
NODE temp;
temp.deep=deep+1;
temp.data=(*key);
tree.push_back(temp);
tree.back().getans(leftnum);
return;
}
tree[son->second].getans(leftnum);
}
void init(void){
gsnum[adjnum]=1;
int i;
for(i=adjnum-1;i>=0;i--){
gsnum[i]=gsnum[i+1]*adjlist[i+1].size();
}
NODE temp;
tree.push_back(temp);
for(i=1;i<=N;i++) root.decnum(i,0);
}
void readline(int x){
string str;
int tot=0;
bool flag=false;
do{
cin>>str;
if(str=="cow.") break;
if(flag){
tot++;
if(adjlist[tot].find(str)==adjlist[tot].end()){
adjlist[tot].insert(str);
}
describe[x].push_back(str);
}
else if(str=="no") flag=true;
}while(str!="cow.");
adjnum=tot;
}
void answer(void){
int i;
for(i=0;i<ans.size();i++) cout<<ans[i]<<" ";
cout<<endl;
}
void feature(int K){
ans.clear();
root.getans(K);
answer();
}
void read(void){
scanf("%d%d",&N,&K);
int i;
for(i=1;i<=N;i++) readline(i);
}
int main(){
freopen("nocow.in","r",stdin);
freopen("nocow.out","w",stdout);
read();
init();
feature(K);
return 0;
}