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;
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 le[2][(int)2e5 + 10];
bool ok[2][(int)2e5 + 10];
void update(int a , int l , int r , int i , int j , int p){
le[p][i].pb(j);
}
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) && !(n > pos + len[cur] && s[pos + len[cur]] == 'X') && 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) && !(n > pos + len[cur] && s[pos + len[cur]] == 'X') && 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);
int idx = 0;
for(int j = 0 ; j < 2 ; j++){
idx = 0;
for(int i = 0 ; i < n ; i++){
if(le[j][i].empty()) continue;
if(idx < i) idx = i;
for(auto e : le[j][i]){
while(idx <= e){
//cout << e << " " << idx << endl;
ok[j][idx] = true; idx++;
}
}
}
}
for(int i = 0 ; i < n ; i++){
if(s[i] != '.') continue;
bool b = ok[1][i];
bool a = ok[0][i];
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... |