题解 AT1978 【[ABC042B] 文字列大好きいろはちゃんイージー / Iroha Loves Strings (ABC Edition)】

· · 题解

优先队列解法

我们来玩一些好玩的。
此种解法可以参照我在 AT2646 撰写的题解。

题目大意

给你 n 个字符串,按字典序排序后输出。

变量介绍

解题过程

首先定义一个字符串类型的小根堆。

priority_queue<string, vector<string >, greater<string > > q;

然后输入 n 个字符串,压入小根堆。

for (int i = 1; i <= n; ++i) {
    string t;
    cin >> t;
    q.push(t);
}

最后只要小根堆非空,输出堆顶元素即可。

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

AC CODE

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <string>
#define int string
using namespace std;

priority_queue<int, vector<int >, greater<int > > q;
long n, l;

signed main() {
    cin >> n >> l;
    for (long i = 1; i <= n; ++i) {
        int t;
        cin >> t;
        q.push(t);
    }
    while (!q.empty()) {
        cout << q.top();
        q.pop();
    }
    putchar('\n');
    return 0;
}

感谢观赏!