1099C - Postcard - CodeForces Solution


constructive algorithms implementation *1200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define txtinput freopen("input.txt", "r", stdin)
#define txtoutput freopen("output.txt", "w", stdout)
#define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
using namespace std;
void safwat()
{
  
   string s;
   cin>>s;
   int k;
   cin>>k;
   int count1=0,count2=0,total=0;
   for(int i=0;i<s.length();i++){
    if(s[i]=='?')count1++;
    if(s[i]=='*')count2++;
   }
   total=s.length()-count1-count2;
   if(k==s.length()&&count2==0&&count1==0){
       cout<<s;
   }else if (count2==0&&k>total){
       cout<<"Impossible";
   }else if(k<total&&count1+count2<total-k){
       cout<<"Impossible";
   }
   else if (k<=total){
       string a=""; 
       int x=total-k;
       for(int i=0;i<s.length();i++){
           if((s[i]=='*'||s[i]=='?')&&x>0){
               a.pop_back();               x--;
           }else if(s[i]!='*'&&s[i]!='?') {
               a+=s[i];
           }
       }
       cout<<a;
   }else if(k>total){
       string a=""; 
       int x=k-total;
       for(int i=0;i<s.length();i++){
           if(s[i]=='*'&&x>0){
               while(x){
                   a+=s[i-1];
                   x--;
               }
           }else  if(s[i]!='*'&&s[i]!='?'){
               a+=s[i];
           }
       }
       cout<<a;
   }
   

}
 
int32_t main()
{
    int t=1;
    //cin >> t;
    while (t--)
    {
        safwat();
       cout << endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences