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;
const int N = 2e5 + 7;
const int K = 1e2 + 7;
pair<bool, bool> dp[N][K];
string s, ans;
vector<int> c;
int v[N];
int a[N], n;
void make_zero(int idx){
if(ans[idx] == '.'){
ans[idx] = '_';
}
else{
if(ans[idx] != '_'){
ans[idx] = '?';
}
}
}
void make_x(int idx){
if(ans[idx] == '.'){
ans[idx] = 'X';
}
else{
if(ans[idx] != 'X'){
ans[idx] = '?';
}
}
}
bool solve(int i1, int i2){
//cout << i1 << " " << i2 << endl;
if(i1 == n){
if(i2 == c.size()){
return 1;
}
return 0;
}
if(dp[i1][i2].second){
return dp[i1][i2].first;
}
dp[i1][i2].second = true;
dp[i1][i2].first = false;
//cout << i1 << " " << i2 << endl;
if(s[i1] == '.' || s[i1] == '_'){
bool b = solve(i1 + 1, i2);
if(b){
make_zero(i1);
dp[i1][i2].first = true;
}
}
if(a[i1] >= c[i2] && (i1 + c[i2] == n || s[i1 + c[i2]] != 'X')){
bool b;
if(i1 + c[i2] == n){
b = solve(i1 + c[i2], i2 + 1);
if(b){
dp[i1][i2].first = true;
v[i1]++;
v[i1 + c[i2]]--;
}
}
else{
b = solve(i1 + c[i2] + 1, i2 + 1);
if(b){
make_zero(i1 + c[i2]);
dp[i1][i2].first = true;
v[i1]++;
v[i1 + c[i2]]--;
}
}
}
return dp[i1][i2].first;
}
string solve_puzzle(string _s, vector<int> _c){
n = _s.size();
s = _s;
for(int i = 0; i < (int)_c.size(); i++){
c.push_back(_c[i]);
}
if(s[n - 1] == 'X' || s[n - 1] == '.'){
a[n - 1] = 1;
}
else{
a[n - 1] = 0;
}
for(int i = n - 2; i >= 0; i--){
if(s[i] == 'X' || s[i] == '.'){
a[i] = a[i + 1] + 1;
}
else{
a[i] = 0;
}
}
//for(int i = 0; i < n; i++){
// cout << a[i] << " ";
//}
//cout << endl;
ans = s;
solve(0, 0);
int curr = 0;
for(int i = 0; i < n; i++){
curr += v[i];
if(curr > 0){
make_x(i);
}
}
return ans;
}
/*int main(){
string _s;
vector<int> _c;
int k;
cin >> _s;
cin >> k;
for(int i = 0; i < k; i++){
int x;
cin >> x;
_c.push_back(x);
}
cout << solve_puzzle(_s, _c) << endl;
return 0;
}*/
/*
..........
2
3 4
*/
Compilation message (stderr)
paint.cpp: In function 'bool solve(int, int)':
paint.cpp:40:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i2 == c.size()){
~~~^~~~~~~~~~~
# | 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... |