WA 求调
查看原帖
WA 求调
1160934
ew3www楼主2025/8/29 19:08

Code

#include <bits/stdc++.h>
using namespace std;
struct node {
    string s;
    int of;
    int id;
} a[100010];
int len = 0;
bool cmp(node x, node y) {
    if (x.of != y.of) {
        return x.of > y.of;
    } else {
        return x.id < y.id;
    }
}
int main() {
    int n, k;
    cin >> n >> k;
    cin.ignore();
    for (int i = 0; i < 3 * n; i++) {
        string tmp;
        getline(cin, tmp);
        if (len == 0) {
            a[0].s = tmp;
            a[0].of = 1;
            a[0].id = 1;
            len++;
        } else {
            int j = 0;
            for ( ; j < len; j++) {
                if (a[j].s == tmp) {
                    a[j].of++;
                    break;
                }
            }
            if (j == len) {
                a[len].s = tmp;
                a[len].of = 1;
                a[len].id = i+1;
                len++;
            }
        }
    }
    sort(a, a + len, cmp);
    for (int i = 0; i < k; i++) {
        cout << a[i].s << endl;
    }
    return 0;
}

题意翻译

给定两个数字 NKN、K,以及 3×N3\times N 个字符串,求出出现次数最多(同样多时按照最先出现的编号降序排序)的 KK 个字符串。

题目传送门

2025/8/29 19:08
加载中...