QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189879 | #2871. Clean Up! | Beevo | WA | 3ms | 8388kb | C++20 | 3.3kb | 2023-09-28 01:13:32 | 2023-09-28 01:13:34 |
Judging History
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);
if (freqToHash[hashToString[hash].size()].empty())
freqToHash.erase(hashToString[hash].size());
hashToString[hash].erase(i);
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();
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 8388kb
input:
4 2 a abc abd b
output:
1
result:
wrong answer 1st lines differ - expected: '2', found: '1'