이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "paint.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
std::string solve_puzzle(std::string s, std::vector<int> c) {
int n = s.size(), k = c.size();
vector<int> prf0 = {0}; // 0
vector<int> prf1 = {0}; // 1
vector<int> rup1(n+1,0);
vector<int> ans0(n+1,0);
for(int i=0;i<n;++i){
prf0.push_back(prf0.back() + (s[i]=='_'));
prf1.push_back(prf1.back() + (s[i]=='X'));
}
vector<vector<bool>> dp(n+1,vector<bool>(k+1,0));
// base cases
dp[n][k] = 1;
for(int j=0;j<k;++j) dp[n][j] = 0;
for(int i=0;i<n;++i){
if (prf1[n]-prf1[i]==0) dp[i][k] = 1;
else dp[i][k] = 0;
}
for(int i=n-1;i>=0;--i){
for(int j=k-1;j>=0;--j){
if (s[i] != '_') { // 1
if (i + c[j] <= n){
if (prf0[i+c[j]]-prf0[i] == 0){
if (i+c[j] != n){
if (s[i+c[j]] != 'X'){
dp[i][j] = (dp[i][j] || dp[i+c[j]+1][j+1]);
}
}
else dp[i][j] = (dp[i][j] || dp[i+c[j]][j+1]);
}
}
}
if (s[i] != 'X') { //0
dp[i][j] = (dp[i][j] || dp[i+1][j]);
}
}
}
//cout << "dp " << dp[0][0] << endl;
// answer extraction
vector<vector<bool>> dp2(n+1,vector<bool>(k+1,0));
dp2[0][0] = 1;
for(int i=0;i<n;++i){
for(int j=0;j<=k;++j){
if (!dp2[i][j] || !dp[i][j]) continue;
if (s[i] != '_' && j != k) { // 1
if (i + c[j] <= n){
if (prf0[i+c[j]]-prf0[i] == 0){
if (i+c[j] != n){
if (s[i+c[j]] != 'X'){
if (dp[i+c[j]+1][j+1]){
dp2[i+c[j]+1][j+1] = 1;
rup1[i]++; rup1[i+c[j]]--; ans0[i+c[j]]=1;
}
}
}
else{
if (dp[i+c[j]][j+1]){
dp2[i+c[j]][j+1] = 1;
rup1[i]++; rup1[i+c[j]]--;
}
}
}
}
}
if (s[i] != 'X') { //0
if (dp[i+1][j]){
dp2[i+1][j] = 1;
ans0[i] = 1;
}
}
}
}
// complete the rupq
for(int i=1;i<n;++i){
rup1[i] += rup1[i-1];
}
string ans;
for(int i=0;i<n;++i){
//cout << "rup1 " << rup1[i] << " ans0 " << ans0[i] << endl;
if (rup1[i] > 0 && ans0[i] > 0){
ans.push_back('?');
}
else if (rup1[i] > 0 ) ans.push_back('X');
else ans.push_back('_');
}
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;
}
*/
# | 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... |