zoukankan      html  css  js  c++  java
  • 合并序列

    原题链接:https://www.luogu.org/problem/show?pid=1628

    看了看题解,怎么大家写的都这么麻烦。。。。我用STL随手瞎搞了一下一遍就过了。。

    这是一个偷懒而不用小根堆的做法。。

    作为专项练习堆的第一题实在是有点水。。

    我们离线处理这些单词。读入之后先扫一遍,如果扫到的单词前缀与给定的相同就压到优先队列里面,然后依次弹出,每次弹出使计数器cnt++。

    然后要怎么办?从cnt到0倒着枚举,输出就好了。。。

    没有想象中那么难。。。。真的

    参考代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 #include <string>
     6 #define maxn 100005
     7 using namespace std;
     8 int n;
     9 string words[maxn];
    10 string outwords[maxn];
    11 priority_queue<string> q;
    12 string st;
    13 int cnt;
    14 
    15 int main(){
    16     scanf("%d",&n);
    17     for (int i=1;i<=n;i++)
    18         cin >> words[i];
    19     cin >> st;
    20     for (int i=1;i<=n;i++){
    21         if (words[i].substr(0,st.length()) == st)
    22             q.push(words[i]);
    23     }
    24     while (!q.empty()){
    25         outwords[++cnt] = q.top();
    26         q.pop();
    27     }
    28     for (int i = cnt;i>=0;i--)
    29         cout << outwords[i] << endl;
    30     return 0;
    31 }
  • 相关阅读:
    2312--1.3.4 Prime Cryptarithm 牛式
    Slava and tanks 877C
    World Cup 996B(排队模拟)
    css内边距 边框
    iframs刷新的两种方法
    JS DOM节点
    JS对话框
    JS事件常用事件
    JS数组
    JS第一天
  • 原文地址:https://www.cnblogs.com/OIerShawnZhou/p/7711587.html
Copyright © 2011-2022 走看看