-
Notifications
You must be signed in to change notification settings - Fork 35
/
1099C - Postcard.cpp
90 lines (78 loc) · 1.6 KB
/
1099C - Postcard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<bits/stdc++.h>
#define dbg(x) if(0)cerr<<#x<<" : "<<x<<endl;
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
string s="",t;
int k;
cin >> t >> k;
int sz = (int) t.size();
bool ok= false;
for(int i = 0 ; i < sz ; i++){
if(t[i]!='*' and t[i]!='?')
ok = true;
if(ok){
s +=t[i];
}
}
int candy=0, snowflake=0;
int n= 0;
for(auto x : s){
if(x=='*')
snowflake++;
else if(x=='?')
candy ++;
else
n++;
}
dbg(candy);
dbg(snowflake);
dbg(n);
if(snowflake==0 && k > n){
cout<<"Impossible\n";
return 0;
}
if(n-(candy+snowflake) > k){
cout<<"Impossible\n";
return 0;
}
int needToDelete = 0 , needTOAdd= 0;
if(k>n){
needTOAdd = k- n;
}
else if(k < n){
needToDelete = n - k;
}
sz =(int) s.size();
int p[sz];
for(int i = 0 ; i < sz; i++){
if(s[i]=='?'){
if(needToDelete){
p[i-1] = 0;
needToDelete--;
}
p[i]=0;
}
else if(s[i]=='*'){
if(needToDelete){
p[i-1] = 0;
needToDelete--;
}
if(needTOAdd){
p[i-1]+=needTOAdd;
needTOAdd = 0;
}
p[i]=0;
}
else
p[i]=1;
}
for(int i = 0; i < sz ; i++){
while(p[i]){
cout<<s[i];
p[i]--;
}
}
cout<<endl;
return 0;
}