QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189880 | #2871. Clean Up! | Beevo | WA | 3ms | 8556kb | C++20 | 3.4kb | 2023-09-28 01:14:42 | 2023-09-28 01:14:42 |
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);
if (freqToHash[hashToString[hash].size()].empty())
freqToHash.erase(hashToString[hash].size());
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);
}
}
cout << sol;
}
signed main() {
Beevo
int t = 1;
// cin >> t;
while (t--)
testCase();
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 8268kb
input:
4 2 a abc abd b
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 3ms
memory: 8388kb
input:
4 2 d c ab a
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 0ms
memory: 8380kb
input:
5 3 please remove all these files
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 8384kb
input:
2 3 c acbabaaccb
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 8324kb
input:
4 1 ccbc bbacb cacbbb caabcbbcba
output:
4
result:
ok single line: '4'
Test #6:
score: 0
Accepted
time: 3ms
memory: 8280kb
input:
10 2 c bbabcacb a cbc acccaaca abcaac abbacc ccb cacbb aaacab
output:
7
result:
ok single line: '7'
Test #7:
score: 0
Accepted
time: 3ms
memory: 8276kb
input:
9 1 aababaaab bc baabbaaacc bcbccbaaaa ac accaab bbbbc aacbaa ab
output:
9
result:
ok single line: '9'
Test #8:
score: -100
Wrong Answer
time: 3ms
memory: 8556kb
input:
5 2 bbaac baac abbbcacab bca ccbbbbccc
output:
4
result:
wrong answer 1st lines differ - expected: '3', found: '4'