1 条题解

  • 0
    @ 2025-8-24 21:35:29

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar Expecto
    **

    搬运于2025-08-24 21:35:28,当前版本为作者最后更新于2016-09-28 13:26:14,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    先把字典排个序,之后lower_bound 下前缀的位置,加上k就好了

    #include<bits/stdc++.h>
    using namespace std;
    pair<string ,int> a[90030];
    bool match(string x,string y){
        if (y.length()>x.length()) return 0;
        return x.substr(0,y.size())==y;
    }
    int main(){
        int w,n;
        cin>>w>>n;
        for (int i=0;i<w;i++){
            cin>>a[i].first;
            a[i].second=i;
        }
        sort(a,a+w);
        while (n--){
            string pre;
            int k;
            cin>>k>>pre;
            int pos=k-1+lower_bound(a,a+w,make_pair(pre,0))-a;
            if (pos>=w || !match(a[pos].first,pre)) {
                cout<<"-1\n";
                continue;
            }
            cout<<a[pos].second+1<<'\n';
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1236
    时间
    1000ms
    内存
    125MiB
    难度
    4
    标签
    递交数
    0
    已通过
    0
    上传者