QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#286348 | #7618. Pattern Search | Misuki# | WA | 190ms | 3520kb | C++20 | 5.2kb | 2023-12-17 18:18:17 | 2023-12-17 18:18:17 |
Judging History
answer
#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
//#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)
namespace R = std::ranges;
namespace V = std::views;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = tuple<int, int, int>;
using ldb = long double;
//#define double ldb
template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
for(const T &X : arr)
os << X << ' ';
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
for(const T &X : vec)
os << X << ' ';
return os;
}
/**
* template name: floorCeilSum
* author: Misuki
* last update: 2023/06/06
* note: g = floor(x / val) is the greatest value s.t. floor(x / g) = val for floor sum,
* g = ceil(x / val) is the least value s.t. ceil(x / g) = val for ceil sum.
* verify: brute forced x <= 1e4
*/
vector<array<int, 3>> calc_floor(int x) {
vector<int> v, rng;
for(int i = x; i; ) {
int val = x / i;
v.emplace_back(val);
rng.emplace_back(x / val);
i = x / (val + 1);
}
rng.emplace_back(0);
vector<array<int, 3>> res;
for(int i = 0; i < v.size(); i++)
res.push_back({v[i], rng[i + 1] + 1, rng[i]});
return res;
}
vector<array<int, 3>> calc_ceil(int x) {
vector<int> v, rng;
for(int i = 1; ; ) {
int val = (x + i - 1) / i;
v.emplace_back(val);
rng.emplace_back((x + val - 1) / val);
if (val == 1)
break;
i = (x + val - 2) / (val - 1);
}
rng.emplace_back(x + 1);
vector<array<int, 3>> res;
for(int i = 0; i < v.size(); i++)
res.push_back({v[i], rng[i], rng[i + 1] - 1});
return res;
}
/**
* template name: zAlgo
* author: Misuki
* last update: 2022/02/04
*/
vector<int> build(string s) {
vector<int> z(ssize(s));
z[0] = s.size();
for(int i = 1, l = 0, r = -1; i < s.size(); i++) {
if (i <= r)
z[i] = min(r - i + 1, z[i - l]);
while(i + z[i] < s.size() and s[i + z[i]] == s[z[i]])
l = i, r = i + z[i], z[i] += 1;
}
return z;
}
array<int, 26> toFreq(string s) {
array<int, 26> res = {};
for(char c : s)
res[c - 'a']++;
return res;
}
string calc(array<int, 26> f, string p) {
string res;
for(int i = 0; f[p[i % ssize(p)] - 'a']; i++)
res += p[i % ssize(p)], f[p[i % ssize(p)] - 'a']--;
return res;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(NULL);
int z; cin >> z;
while(z--) {
string s, t; cin >> s >> t;
array<int, 26> fs = toFreq(s), ft = toFreq(t);
array<map<int, int>, 26> cand;
for(int i = 0; i < 26; i++) {
if (ft[i] > 0) {
for(auto [x, l, __] : calc_ceil(ft[i])) {
cand[i][x] = l;
if (ft[i] % x == 0 and !cand[i].contains(x + 1))
cand[i][x + 1] = l;
}
}
}
auto check = [&](int x) {
for(int i = 0; i < 26; i++)
if (ft[i] > 0 and !cand[i].contains(x))
return false;
return true;
};
int p = 1;
for(int i = 2; i <= ssize(t); i++)
if (check(i))
p = i;
array<int, 26> cnt = {};
for(int i = 0; i < 26; i++)
if (ft[i] > 0)
cnt[i] = cand[i][p];
string r;
{
array<int, 26> re = cnt;
for(int i = 0; i < 26; i++) {
if (cnt[i] == 0) continue;
for(int j = 0; j < ft[i] % cnt[i]; j++)
r += (char)('a' + i);
re[i] -= ft[i] % cnt[i];
}
for(int i = 0; i < 26; i++)
while(re[i]--)
r += (char)('a' + i);
}
string ss = calc(fs, r);
string tt = calc(ft, r);
auto z = build(tt + '#' + ss);
int ans = 0;
for(int i = ssize(tt) + 1; i < ssize(z); i++)
ans += z[i] == ssize(tt);
cout << ans << '\n';
//cout << r << '\n';
//cout << ss << '\n';
//cout << tt << '\n';
}
//cout << calc_ceil(10) << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3520kb
input:
2 bajkaaall aal abca cba
output:
2 1
result:
ok 2 number(s): "2 1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3440kb
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
Wrong Answer
time: 190ms
memory: 3500kb
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...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 1 1 1 2 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 2 1 1 1 1 1 2 4 1 1 1 1 1 1 1 1 1 2 2 2 2 1 1 1 3 1 1 1 1 4 1 2 1 1 1 1 1 3 2 1 3 1 1 1 1 1 1 1 1 1 1 1 3 1 1 4 1 1 1 1 1 1 1 1 1 1 5 1 7 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 2 ...
result:
wrong answer 43rd numbers differ - expected: '1', found: '2'