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>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define dte tuple<double , double , double>
using namespace std;
const int INF = 1000*1000*1000; // 1 e 9
const int MOD = 1e9 + 7;//998244353 ;
const double EPS = 0.000000001; // 1 e -9
const ll inf = (ll)1e18;
bool lazy[2][(int)8e5 + 10];
bool st[2][(int)8e5 + 10];
void update(int pos , int l , int r , int i , int j , int p){
if(lazy[p][pos]){
st[p][pos] = true;
if(l != r){
lazy[p][2 * pos + 1] = lazy[p][2 * pos + 2] = true;
}
lazy[p][pos] = false;
}
if(i > r || j < l) return;
if(i<= l && j >= r){
st[p][pos] = true;
if(l != r){
lazy[p][2 * pos + 1] = lazy[p][2 * pos + 2] = true;
}
return;
}
update(2 * pos + 1 , l , (l+r) / 2 , i , j , p); update(2 * pos + 2 , (l+r) / 2 + 1 , r, i ,j , p);
st[p][pos] = st[p][2 * pos + 1] && st[p][2 * pos + 2];
}
bool rq(int pos , int l , int r , int i , int p){
if(lazy[p][pos]){
st[p][pos] = true;
if(l != r){
lazy[p][2 * pos + 1] = lazy[p][2 * pos + 2] = true;
}
lazy[p][pos] = false;
}
if(i > r || i < l) return false;
if(l == r || st[p][pos]) return st[p][pos];
return rq(2 * pos + 1 , l , (l+r) / 2 , i , p) || rq(2 * pos + 2 , (l+r) / 2 + 1 , r , i , p);
}
int k , n;
string s;
int nb[(int)2e5 + 10];
int cnt[(int)2e5 + 10];
bool dp[(int)2e5 + 10][101];
bool vis[(int)2e5 + 10][101];
vi len;
bool solve(int pos , int cur){
//cout << pos << " " << cur << endl;
if(pos >= n)return cur == k;
if(cur == k) {
if(cnt[n] - cnt[pos] == 0){
update(0 , 0 , n - 1 , pos , n - 1 , 1);
return true;
}
return false;
}
if(vis[pos][cur]) return dp[pos][cur];
vis[pos][cur] = true;
if(s[pos] != '.'){
if(s[pos] == '_') return dp[pos][cur] = solve(pos + 1 , cur);
else {
bool ans = (n >= pos + len[cur]) && (nb[pos + len[cur]] - nb[pos] == 0) && solve(pos + len[cur] + 1, cur + 1);
if(ans){
update(0 , 0 , n -1 , pos , pos + len[cur] - 1 , 0);
if(n > pos + len[cur]) update(0 , 0 , n- 1 , pos + len[cur] , pos + len[cur], 1);
}
return dp[pos][cur] = ans;
}
}
bool a = solve(pos + 1 , cur);
bool ans = (n >= pos + len[cur]) && (nb[pos + len[cur]] - nb[pos] == 0) && solve(pos + len[cur] + 1 , cur + 1);
if(ans){
update(0 , 0 , n -1 , pos , pos + len[cur] - 1 , 0);
if(n > pos + len[cur]) update(0 , 0 , n- 1 , pos + len[cur] , pos + len[cur] , 1);
}
if(a) update(0 , 0 , n - 1 , pos , pos , 1);
//cout << pos << " " << cur << " " << a << " " << ans << endl;
return dp[pos][cur] = a || ans;
}
string solve_puzzle(string w, vi c){
k = c.size(); s = w; n = s.length(); len = c;
for(int i = 1 ; i <= n ; i++) nb[i] = nb[i - 1] + (s[i - 1] == '_');
for(int i = 1 ; i <= n ; i++) cnt[i] = cnt[i - 1] + (s[i - 1] == 'X');
//for(auto e : len) cout << e << " "; cout << endl;
solve(0 , 0);
for(int i = 0 ; i < n ; i++){
if(s[i] != '.') continue;
bool a = rq(0 , 0 , n - 1 , i , 0);
bool b = rq(0 , 0 , n - 1 , i , 1);
if(a && b) s[i] = '?';
else if(a) s[i] = 'X';
else{
assert(b);
s[i] = '_';
}
}
return s;
}
/*int main(){
//ifstream fin ("testing.txt");
//ofstream fout ("output.txt");
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
string w; cin>>w;
int kk; cin>>kk;
vi c(kk); for(int i = 0 ; i < kk ; i++) cin>>c[i];
cout << solve_puzzle(w , c);
return 0;
}
*/
/*
Think of : BS / DFS / BFS / SSSP / SCC / MSP / MAX FLOW / TOPSORT / LCA / MATRIX / DP(bitmask) / 2 POINTERS / SEG TREE / MATH / UN FIND / MO / HLD
Read the statement CAREFULLY !!
Make a GREADY APPROACH !!!! (start from highest / lowest)
Make your own TESTS !!
Be careful from CORNER CASES !
*/
# | 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... |