P1628 合并序列

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n;

priority_queue<string,vector<string>,greater<string>> hp,res;

int main()
{
    cin>>n;
    while(n--){
        string s;
        cin>>s;
        hp.push(s);
    }

    string t;
    cin>>t;
    while(!hp.empty()){
        string s=hp.top();
        hp.pop();
        if(s.find(t)==0) res.push(s);
    }

    while(!res.empty()){
        cout<<res.top()<<endl;
        res.pop();
    }

    return 0;
}
堆还可以存储string,而且自动排序,sort都省了,还学会了一个判断是否为前缀的方法,string.find(x,y)第一个参数是字符串,第二个参数是开始位置,默认为0,返回的是匹配成功的位置,find==0就是前缀了
posted @ 2025-12-02 16:43  gosaky  阅读(0)  评论(0)    收藏  举报