This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
return y > x ? (x = y, true) : false;
}
void debug_out() {
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
cerr << ' ' << A;
debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
for (int i = 0; i < n; ++i) cerr << a[i];
cerr << endl;
}
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
for (auto& x : v) {
stream << x << ' ';
}
return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
return stream << p.first << ' ' << p.second;
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
const int MOD = 998244353;
struct fp {
int a;
fp(): a(0) {}
fp(ll x) {
a = x % MOD;
if (a < 0) {
a += MOD;
}
}
fp(const fp& oth): a(oth.a) {}
fp& operator += (const fp& oth) {
if ((a += oth.a) >= MOD) {
a -= MOD;
}
return *this;
}
fp& operator -= (const fp& oth) {
if ((a -= oth.a) < 0) {
a += MOD;
}
return *this;
}
fp& operator *= (const fp& oth) {
a = a * (ll)oth.a % MOD;
return *this;
}
bool operator == (const fp& oth) {
return a == oth.a;
}
};
fp operator + (const fp& lhs, const fp& rhs) {
return fp(lhs) += rhs;
}
fp operator - (const fp& lhs, const fp& rhs) {
return fp(lhs) -= rhs;
}
fp operator * (const fp& lhs, const fp& rhs) {
return fp(lhs) *= rhs;
}
ostream& operator << (ostream& stream, const fp& f) {
return stream << f.a;
}
const int maxN = 200005;
const int maxK = 102;
const char code[4] = "X_.";
fp pref[maxN][maxK];
fp suff[maxN][maxK];
void rec(vector<int> s, vector<int> c, fp dp[][maxK]) {
int n = s.size();
int K = c.size();
vector<int> cnt[2];
for (int x = 0; x < 2; ++x) {
cnt[x].assign(n + 1, 0);
for (int i = 0; i < n; ++i) {
cnt[x][i + 1] = cnt[x][i] + (s[i] == x);
}
}
auto gt = [&](int x, int l, int r) {
return cnt[x][r + 1] - cnt[x][l];
};
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
if (s[i - 1] != 1) {
for (int k = 1; k <= K; ++k) {
int len = c[k - 1];
if (len <= i && gt(1, i - len, i - 1) == 0 && (i - len - 1 == -1 || s[i - len - 1] != 0)) {
dp[i][k] += dp[max(i - len - 1, 0)][k - 1];
}
}
}
if (s[i - 1] != 0) {
for (int k = 0; k <= K; ++k) {
dp[i][k] += dp[i - 1][k];
}
}
/*debug(i);
for (int k = 0; k <= K; ++k) {
cerr << dp[i][k].a << ' ';
}
cerr << endl;*/
}
/*debug(dp[n][K].a);
exit(0);*/
}
string solve_puzzle(string _s, vector<int> c) {
int n = _s.size();
int K = c.size();
vector<int> s(n);
for (int i = 0; i < n; ++i) {
s[i] = find(code, code + 3, _s[i]) - code;
}
// debug(s);
// debug(c);
rec(s, c, pref);
reverse(all(s));
reverse(all(c));
rec(s, c, suff);
reverse(all(s));
reverse(all(c));
fp A = pref[n][K];
string ans(n, '?');
for (int i = 0; i < n; ++i) {
if (s[i] != 2) {
ans[i] = code[s[i]];
} else {
fp white = 0;
for (int l = 0; l <= K; ++l) {
int r = K - l;
white += pref[i][l] * suff[n - i - 1][r];
}
// debug(i, white.a, A.a);
if (white == A) {
ans[i] = code[1];
} else if (white == 0) {
ans[i] = code[0];
} else {
ans[i] = '?';
}
}
}
return ans;
}
#ifdef DEBUG
int32_t main() {
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int k;
cin >> k;
vector<int> c(k);
for (auto& x : c) cin >> x;
cout << solve_puzzle(s, c) << '\n';
return 0;
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |