QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#189877#2871. Clean Up!BeevoWA 3ms8340kbC++203.3kb2023-09-28 01:12:312023-09-28 01:12:32

Judging History

你现在查看的是最新测评结果

  • [2023-09-28 01:12:32]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:8340kb
  • [2023-09-28 01:12:31]
  • 提交

answer

#include <bits/stdc++.h>

#define el '\n'
#define Beevo ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

typedef long long ll;
typedef long double ld;

using namespace std;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const int P = 31, P2 = 37, M = 1e9 + 7, N = 3e5 + 5;

int pw[N], pw2[N], inv[N], inv2[N];

int add(int a, int b) {
    b = (b + M) % M;

    return (a + b) % M;
}

int mul(int a, int b) {
    return 1LL * a * b % M;
}

int modPow(int b, int p) {
    if (p == 0)
        return 1;

    int x = modPow(b, p / 2);

    return p % 2 == 0 ? mul(x, x) : mul(b, mul(x, x));
}

int modInvFer(int n) {
    return modPow(n, M - 2);
}

void pre() {
    pw[0] = inv[0] = pw2[0] = inv2[0] = 1;

    int invP = modInvFer(P), invP2 = modInvFer(P2);

    for (int i = 1; i < N; i++) {
        pw[i] = mul(pw[i - 1], P);
        inv[i] = mul(inv[i - 1], invP);

        pw2[i] = mul(pw2[i - 1], P2);
        inv2[i] = mul(inv2[i - 1], invP2);
    }
}

struct Hash {
    vector<pair<int, int>> prefixHash;

    Hash(string& s) {
        prefixHash = vector<pair<int, int>> (s.size());

        for (int i = 0; i < s.size(); i++) {
            prefixHash[i].first = mul(s[i] - 'a' + 1, pw[i]);
            prefixHash[i].second = mul(s[i] - 'a' + 1, pw2[i]);

            if (i) {
                prefixHash[i].first = add(prefixHash[i].first, prefixHash[i - 1].first);
                prefixHash[i].second = add(prefixHash[i].second, prefixHash[i - 1].second);
            }
        }
    }

    pair<int, int> getHash(int l, int r) {
        return {
                mul(add(prefixHash[r].first, (l ? -prefixHash[l - 1].first : 0)), inv[l]),
                mul(add(prefixHash[r].second, (l ? -prefixHash[l - 1].second : 0)), inv2[l])
        };
    }
};

map<int, set<pair<int, int>>> freqToHash;
map<pair<int, int>, set<int>> hashToString;

void add(pair<int, int> hash, int i) {
    freqToHash[hashToString[hash].size()].erase(hash);

    hashToString[hash].insert(i);

    freqToHash[hashToString[hash].size()].insert(hash);
}

void remove(pair<int, int> hash, int i) {
    freqToHash[hashToString[hash].size()].erase(hash);

    hashToString[hash].erase(i);

    if (hashToString[hash].empty())
        hashToString.erase(hash);

    freqToHash[hashToString[hash].size()].insert(hash);
}

void testCase() {
    pre();

    int n, k;
    cin >> n >> k;

    string s[n];
    vector<Hash> hashes;
    for (int i = 0; i < n; i++) {
        cin >> s[i], hashes.push_back(Hash(s[i]));

        for (int j = 0; j < s[i].size(); j++)
            add(hashes[i].getHash(0, j), i);
    }

    int cnt = n, sol = 0;
    while (cnt) {
        sol++;

        if (cnt <= k)
            break;

        auto up = freqToHash.upper_bound(k);

        up--;

        auto hash = *up->second.begin();

        while (hashToString[hash].size()) {
            cnt--;

            int i = *hashToString[hash].begin();

            for (int j = 0; j < s[i].size(); j++)
                remove(hashes[i].getHash(0, j), i);
        }

        break;
    }

    cout << sol;
}

signed main() {
    Beevo

    int t = 1;
//    cin >> t;

    while (t--)
        testCase();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 8340kb

input:

4 2
a
abc
abd
b

output:

1

result:

wrong answer 1st lines differ - expected: '2', found: '1'