QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#383179 | #5414. Stop, Yesterday Please No More | shepherd | RE | 1ms | 4052kb | C++20 | 8.4kb | 2024-04-09 02:57:27 | 2024-04-09 02:57:27 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#ifdef LLOCAL
#include "debug.h"
#else
#define var(...)
#define debugArr(...)
#endif
using namespace std;
#define len(a) static_cast<int>((a).size())
#define present(c, x) (c.find(x) != c.end())
#define printDecimal(d) std::cout << std::setprecision(d) << std::fixed
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr const int iinf = 1e9 + 7;
constexpr const ll inf = 1e18;
constexpr const ll mod = 1'000'000'007;
template <typename Fun>
class y_combinator_result {
Fun fun_;
public:
template <typename T>
explicit y_combinator_result(T&& fun) : fun_(std::forward<T>(fun)) {}
template <typename... Args>
decltype(auto) operator()(Args&&... args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <typename Fun>
decltype(auto) y_combinator(Fun&& fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
pair<vector<vector<int>>, int> construct_grid(int n, int m, const string& s) {
vector<vector<int>> grid_2d(n, vector<int>(m));
int u = 0, l = 0, r = m - 1, d = n - 1;
int bu = 0, bl = 0, br = m - 1, bd = n - 1;
for (auto c : s) {
switch (c) {
case 'U': {
if (bu == u) {
bu++;
}
u++, d++;
break;
}
case 'L': {
if (bl == l) {
bl++;
}
l++, r++;
break;
}
case 'R': {
if (br == r) {
br--;
}
r--, l--;
break;
}
case 'D': {
if (bd == d) {
bd--;
}
d--, u--;
break;
}
default: {
assert(false);
}
}
}
int oob = n * m;
if (bu <= bd && bl <= br) {
for (int i = bu; i <= bd; i++) {
for (int j = bl; j <= br; j++) {
grid_2d[i][j] = 1;
oob--;
}
}
}
return make_pair(grid_2d, oob);
}
vector<ll> flatten_and_cast(const vector<vector<int>>& grid) {
vector<ll> ret;
for (const auto& elem : grid) {
for (auto& x : elem) {
ret.push_back(x);
}
}
return ret;
}
tuple<vector<ll>, int, int, int, int> construct_motif(int n, int m,
const string& s) {
int px = 0, py = 0, cx = 0, cy = 0;
for (auto c : s) {
switch (c) {
case 'D': {
if (cx == 0)
px++;
else
cx--;
break;
}
case 'U': {
cx++;
break;
}
case 'R': {
if (cy == 0)
py++;
else
cy--;
break;
}
case 'L': {
cy++;
break;
}
}
}
vector<vector<int>> motif(px + 1, vector<int>(px + 1));
cx = px, cy = py;
motif[cx][cy] = 1;
int num_col = py + 1;
for (auto c : s) {
switch (c) {
case 'D': {
cx--;
assert(cx >= 0);
break;
}
case 'U': {
cx++;
break;
}
case 'L': {
cy++;
break;
}
case 'R': {
cy--;
assert(cy >= 0);
break;
}
default: {
assert(false);
}
}
while (len(motif) <= min(cx, n - 1)) motif.emplace_back();
while (cx < n && len(motif[cx]) <= min(cy, m - 1)) motif[cx].push_back(0);
if (cx < n && cy < m) {
motif[cx][cy] = 1;
num_col = max(num_col, cy + 1);
}
}
for (int i = 0; i < len(motif); i++) {
while (len(motif[i]) < num_col) motif[i].push_back(0);
if (i != len(motif) - 1) {
while (len(motif[i]) < m + py * 2) motif[i].push_back(0);
}
}
return make_tuple(flatten_and_cast(motif), px, py, len(motif), num_col);
}
vector<vector<int>> pad_grid(const vector<vector<int>>& grid, int pt, int pb,
int pl, int pr) {
vector<vector<int>> ret;
for (int i = 0; i < pt; i++) {
ret.emplace_back();
for (int j = 0; j < pl; j++) {
ret.back().push_back(0);
}
for (int j = 0; j < len(grid[0]); j++) {
ret.back().push_back(0);
}
for (int j = 0; j < pr; j++) {
ret.back().push_back(0);
}
}
for (int i = 0; i < len(grid); i++) {
ret.emplace_back();
for (int j = 0; j < pl; j++) {
ret.back().push_back(0);
}
for (int j = 0; j < len(grid[i]); j++) {
ret.back().push_back(grid[i][j]);
}
for (int j = 0; j < pr; j++) {
ret.back().push_back(0);
}
}
for (int i = 0; i < pb; i++) {
ret.emplace_back();
for (int j = 0; j < pl; j++) {
ret.back().push_back(0);
}
for (int j = 0; j < len(grid[0]); j++) {
ret.back().push_back(0);
}
for (int j = 0; j < pr; j++) {
ret.back().push_back(0);
}
}
return ret;
}
namespace kactl {
typedef vector<ll> vl;
typedef complex<double> C;
void fft(vector<C>& a) {
int n = len(a), L = 31 - __builtin_clz(n);
static vector<complex<long double>> R(2, 1);
static vector<C> rt(2, 1); // (^ 10% faster if double)
for (static int k = 2; k < n; k *= 2) {
R.resize(n);
rt.resize(n);
auto x = polar(1.0L, acos(-1.0L) / k);
for (int i = k; i < 2 * k; i++)
rt[i] = R[i] = i & 1 ? R[i / 2] * x : R[i / 2];
}
vector<int> rev(n);
for (int i = 0; i < n; i++) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
for (int i = 0; i < n; i++)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int k = 1; k < n; k *= 2)
for (int i = 0; i < n; i += 2 * k)
for (int j = 0; j < k; j++) {
// C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) ///
// include-line
auto x = (double*)&rt[j + k],
y = (double*)&a[i + j + k]; /// exclude-line
C z(x[0] * y[0] - x[1] * y[1],
x[0] * y[1] + x[1] * y[0]); /// exclude-line
a[i + j + k] = a[i + j] - z;
a[i + j] += z;
}
}
template <int M>
vl convMod(const vl& a, const vl& b) {
if (a.empty() || b.empty()) return {};
vl res(len(a) + len(b) - 1);
int B = 32 - __builtin_clz(len(res)), n = 1 << B, cut = int(sqrt(M));
vector<C> L(n), R(n), outs(n), outl(n);
for (int i = 0; i < len(a); i++) L[i] = C((int)a[i] / cut, (int)a[i] % cut);
for (int i = 0; i < len(b); i++) R[i] = C((int)b[i] / cut, (int)b[i] % cut);
fft(L), fft(R);
for (int i = 0; i < n; i++) {
int j = -i & (n - 1);
outl[j] = (L[i] + conj(L[j])) * R[i] / (2.0 * n);
outs[j] = (L[i] - conj(L[j])) * R[i] / (2.0 * n) / 1i;
}
fft(outl), fft(outs);
for (int i = 0; i < len(res); i++) {
ll av = ll(real(outl[i]) + .5), cv = ll(imag(outs[i]) + .5);
ll bv = ll(imag(outl[i]) + .5) + ll(real(outs[i]) + .5);
res[i] = ((av % M * cut + bv) % M * cut + cv) % M;
}
return res;
}
} // namespace kactl
struct wildcard_matching {
vector<ll> p, t;
wildcard_matching(const vector<ll>& T, const vector<ll>& P) : p(P), t(T) {}
int operator()(int n, int m, int mn, int mm, int target) {
reverse(p.begin(), p.end());
auto d = kactl::convMod<mod>(t, p);
// var(p);
// var(t);
// var(d);
// var(mn, mm, n, m, target);
int ret = 0;
for (int i = 0; i + mn <= n; i++) {
for (int j = 0; j + mm <= m; j++) {
// var(d[i * m + j + len(p) + 1]);
if (d[i * m + j + len(p) + 1] == target) {
ret++;
}
}
}
return ret;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, m, k;
cin >> n >> m >> k;
string s;
cin >> s;
auto [grid, oob] = construct_grid(n, m, s);
auto [motif_1d, px, py, mn, mm] = construct_motif(n, m, s);
int pad_bottom = 0, pad_right = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
pad_bottom = max(pad_bottom, i + mn - px - 1 - len(grid) + 1);
pad_right = max(pad_right, j + mm - py - 1 - len(grid[0]) + 1);
}
}
grid = pad_grid(grid, px, pad_bottom, py, pad_right);
int target = n * m - oob - k;
if (target < 0) {
cout << 0 << '\n';
continue;
}
n = len(grid), m = len(grid[0]);
// grid = pad_grid(grid, max(mn - len(grid), 0), max(mm - len(grid[0]), 0));
auto grid_1d = flatten_and_cast(grid);
cout << wildcard_matching{grid_1d, motif_1d}(n, m, mn, mm, target) << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4052kb
input:
3 4 5 3 ULDDRR 4 5 0 UUUUUUU 4 5 10 UUUUUUU
output:
2 20 0
result:
ok 3 number(s): "2 20 0"
Test #2:
score: -100
Runtime Error
input:
1060 19 12 0 UDLDDUUUUDDDLLRDUDUURULUUUDRDUDRDRLRLRLULULLLDLDDRLUUUURUUUDDRLLRUUUDULURUULLRDRLRDDURDUUURRRLURLRUULRRUDURDLUUURDLURDDLUUURDDRLLURRDLRUDLRDRLLRRDRDDLDRURRRLUDULLLRUUDLRRURRDLLRRRDLLRDDDLRLRURURDDDL 11 1 0 UR 3 18 33 UDRLR 17 11 132 RLDRDLDRUU 6 10 13 UULUDDLRDLUUDLDD 1 15 0 D 6 20 50 D...