P2237 [USACO14FEB]自动完成Auto-complete

Posted wsmrxc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

以上是关于P2237 [USACO14FEB]自动完成Auto-complete的主要内容,如果未能解决你的问题,请参考以下文章

P3102 [USACO14FEB]秘密代码Secret Code

[USACO14FEB]路障Roadblock

[USACO14FEB]路障Roadblock

bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

P2176 [USACO14FEB]路障Roadblock

3940. [USACO15FEB]CensoringAC自动机+栈