QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#502301 | #7618. Pattern Search | pandapythoner | RE | 0ms | 3592kb | C++23 | 3.5kb | 2024-08-03 03:21:34 | 2024-08-03 03:21:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) for(int i = 0; i < n; i += 1)
#define len(a) ((int)(a).size())
const ll inf = 1e18;
mt19937 rnd(234);
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int t;
cin >> t;
rep(itr, t) {
string s, t;
cin >> s >> t;
array<int, 26> cs, ct;
cs.fill(0); ct.fill(0);
for (auto x : s) cs[x - 'a'] += 1;
for (auto x : t) ct[x - 'a'] += 1;
int result = 0;
bool ok = true;
rep(i, 26) if (cs[i] < ct[i]) ok = false;
if (!ok) {
cout << 0 << "\n";
continue;
}
for (int k = 1; k <= len(t); k += 1) {
int blocks = len(t) / k;
int last_block = len(t) % k;
int rest = last_block;
array<int, 26> cur_in_block, cur_in_last_block;
bool ok = true;
rep(i, 26) {
cur_in_block[i] = ct[i] / blocks;
cur_in_last_block[i] = ct[i] % blocks;
if (cur_in_last_block[i] > cur_in_block[i]) ok = false;
rest -= cur_in_last_block[i];
}
if (!ok or rest < 0 or rest % blocks != 0) continue;
int todo = rest / blocks;
int max_diff = 0;
rep(j, 26) {
max_diff += (cur_in_block[j] - cur_in_last_block[j]) / (blocks + 1);
}
while (todo > 0) {
int mn = len(s) + 1;
int mn2 = len(s) + 1;
int mnj = -1;
rep(j, 26) {
if (cur_in_block[j] == 0) continue;
if (cur_in_block[j] - 1 < cur_in_last_block[j] + blocks) continue;
assert(cs[j] - cur_in_last_block[j] >= 0);
int val = (cs[j] - cur_in_last_block[j]) / cur_in_block[j];
if (val < mn) {
mn2 = mn;
mn = val;
mnj = j;
} else {
mn2 = min(mn2, val);
}
}
if (mnj == -1) {
ok = false;
break;
}
ll max_good_diff = min(todo, (cur_in_block[mnj] - cur_in_last_block[mnj]) / (blocks + 1));
assert(max_good_diff > 0);
ll can_do = 1;
if (mn2 - blocks > 0)
can_do = (mn2 * cur_in_block[mnj] + cur_in_last_block[mnj] - cs[mnj]) / (mn2 - blocks) + 1;
assert(can_do > 0);
ll really_do = min(max_good_diff, can_do);
cur_in_block[mnj] -= really_do;
cur_in_last_block[mnj] += really_do * blocks;
todo -= really_do;
}
if (!ok) continue;
int cur_result = len(s) + 1;
rep(j, 26) {
if (cur_in_block[j] == 0) {
assert(cur_in_last_block[j] == 0);
continue;
}
cur_result = min(cur_result, (cs[j] - cur_in_last_block[j]) / cur_in_block[j] - blocks + 1);
}
result = max(result, cur_result);
}
cout << result << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3592kb
input:
2 bajkaaall aal abca cba
output:
2 1
result:
ok 2 number(s): "2 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3484kb
input:
16 a a a b b a aa a ab aa ab b ab c aaz az abcde edcba aaaaaaaaaaaabbb aaaaaaaaabb aaaaaazz az aaaaaaaaaz zzzzz gggggggggggggggggggge ggggeeee hyphyphyphyphyphyphyphyphyphyphyphyp eeeeeeeeee hyphyphyphyphyphyphyphyphyphyphyphype eeteeteeteet aaaabbbbbbcccccccc aaabbbbbcccccc
output:
1 0 0 2 0 1 0 1 1 2 2 0 0 0 0 1
result:
ok 16 numbers
Test #3:
score: -100
Runtime Error
input:
90522 cyykzyylklyll ylcyllklzk ttusuuudtdtqus uuddu uefyqfkiblyfkyd ffyyqde qfxqecljeqeedea jqdxf prrbfxdxffpbpp ffppd ynjgygygjnjnjg jgynjggn maenpaksmxyya saxkep nrdnbnjipnjowjz djbwojzrpni oputuoufoojupu uoouopo mphmhphpkpkpmhp phmhpppp zwznzpzqyjczzy wczjnpzqy pfxfxxkfffpfx fxffkffxpx hzdhzhhh h...