P2237 [USACO14FEB]自动完成Auto-complete

先排个序, 对于每一个询问二分前缀位置, 加上k即可. 莫名其妙WA了一次居然是因为
ios::sync_with_stdio(false);

(puts();)有影响...

#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<string, int> P;
const int MAXN = 3e4 + 10;

int W, N;
P s[MAXN];

int main()
{
	ios::sync_with_stdio(false);
	cin>>W>>N;
	for(int i = 1; i <= W; i++) cin>>s[i].first, s[i].second = i;
	sort(s + 1, s + W + 1);

	for(int i = 1; i <= N; i++){
		int k; string str;
		cin>>k>>str;
		P *pit = lower_bound(s + 1, s + W + 1, P(str, 0)) + k - 1;
		if(pit - s <= W && pit->first.substr(0, str.size()) == str)
			cout<<pit->second<<endl;
		else cout<<-1<<endl;
	}

	return 0;
}
原文地址:https://www.cnblogs.com/wsmrxc/p/9548697.html