如题
代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
struct node{
string str;
int index;
};
bool cmp(node a,node b){
string sa = a.str,sb = b.str;
int i = 0;
while(sa[i]==sb[i]){
i++;
if (i==sa.length()) return true;
if (i==sb.length()) return false;
}
return sa[i]<=sb[i];
}
int main(){
int n,m;
cin>>n>>m;
node dict[n];
for (int i = 0;i<n;i++){
cin>>dict[i].str;
dict[i].index = i;
}
sort(dict,dict+n,cmp);
while(m--){
string s;
int k;
cin>>k>>s;
int l = 0;
while(dict[l].str.find_first_of(s)!=0) l++;
if (dict[l+k-1].str.find_first_of(s)==0){
cout<<dict[l+k-1].index+1<<"\n";
}
else {
cout<<"-1\n";
}
}
return 0;
}