1. 程式人生 > >P2237 [USACO14FEB]自動完成Auto-complete

P2237 [USACO14FEB]自動完成Auto-complete

span 二分 -c bound 一次 space cout out 完成


先排個序, 對於每一個詢問二分前綴位置, 加上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