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>
#include "paint.h"
using namespace std;
vector<int>v;
vector<int>prefX, prefW;
vector<int>le,ri;
string s;
int n,k;
bool dfs(int i, int x) {
if(i == k)
return 1;
if(x >= n || x + v[i] > n)
return 0;
bool ok;
if(prefW[x+v[i]] == prefW[x] && (x + v[i] == n || s[x+v[i]] != 'X')) {
ok = dfs(i+1, x + v[i] + 1 );
}
if(ok) {
le[i] = x;
return 1;
}
return dfs(i, x+1);
}
bool rdfs(int i, int x) {
if(i == -1)
return 1;
if(x < 0 || x - v[i] < -1)
return 0;
bool ok;
if(prefW[x+1] == prefW[x - v[i] + 1] && (x - v[i] == -1 || s[x - v[i]] != 'X')) {
ok = rdfs(i-1, x - v[i] - 1);
}
if(ok) {
ri[i] = x - v[i] + 1;
return 1;
}
return rdfs(i, x-1);
}
string solve_puzzle(string S, vector<int> c) {
v = c;
s = S;
n = (int)s.length();
k = (int)c.size();
prefX.resize(n+1);
prefW.resize(n+1);
le.resize(n+1);
ri.resize(n+1);
prefX[0] = prefW[0] = 0;
for(int i = 0 ; i < n ; i++) {
prefX[i+1] = prefX[i] + (v[i] == 'X' ? 1 : 0);
prefW[i+1] = prefW[i] + (v[i] == '_' ? 1 : 0);
}
dfs(0, 0);
rdfs(k-1, n-1);
for(int i = 0 ; i < k ; i++) {
//cout << le[i] << " " << ri[i] << '\n';
}
string ans = "";
for(int i = 0 ; i < n ; i++) {
ans += '_';
}
for(int i = 0 ; i < k ; i++) {
if(le[i] + v[i] - 1 >= ri[i]) {
for(int ii = ri[i] ; ii <= le[i] + v[i] - 1 ; ii++) {
ans[ii] = 'X';
}
}
//cout << i << " " << ans << '\n';
for(int ii = ri[i] ; ii < ri[i] + v[i] ; ii++) {
if(s[ii] != 'X' && ans[ii] != 'X') {
ans[ii] = '?';
}
}
for(int ii = le[i] ; ii <= ri[i] ; ii++) {
if(prefW[ii + v[i]] == prefW[ii] && v[ii + v[i]] != 'X' && v[ii-1] != 'X' && s[ii] != 'X' && ans[ii] != 'X') {
for(int j = ii ; j < ii + v[i] ; j++) {
ans[j] = '?';
}
}
}
}
for(int i = 0 ; i < n ; i++) {
if(s[i] == 'X')
ans[i] = 'X';
}
return ans;
}
/*
#include <vector>
#include <string>
#include <cstdio>
#include <cassert>
const int S_MAX_LEN = 200 * 1000;
char buf[S_MAX_LEN + 1];
int main() {
assert(1 == scanf("%s", buf));
std::string s = buf;
int c_len;
assert(1 == scanf("%d", &c_len));
std::vector<int> c(c_len);
for (int i = 0; i < c_len; i++) {
assert(1 == scanf("%d", &c[i]));
}
std::string ans = solve_puzzle(s, c);
printf("%s\n", ans.data());
return 0;
}
*/
Compilation message (stderr)
paint.cpp: In function 'bool dfs(int, int)':
paint.cpp:22:3: warning: 'ok' may be used uninitialized in this function [-Wmaybe-uninitialized]
22 | if(ok) {
| ^~
paint.cpp: In function 'bool rdfs(int, int)':
paint.cpp:38:3: warning: 'ok' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | if(ok) {
| ^~
# | 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... |