CHDIGER

closed account (STD8C542)
https://www.codechef.com/MARCH19B/problems/CHDIGER


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
  #include <bits/stdc++.h>
using namespace std;


int main()
{
    int t; long long int n, l;
    int d;
    cin >> t;
    while(t--){
        cin >> n >> d; 
        stringstream ss;
        ss << n;
        string s1=ss.str(); //converting n to string
        int l1 = s1.length(); vector<int>c; char a[l1];
        stringstream s;
        s << d;
        string s2=s.str(); //converting d to string
        vector<char> v(s1.begin(), s1.end()); //converting string of n to a vector of char
        char sml = *min_element(v.begin(), v.end()); //smallest number in vector
        int min_pos = distance(v.begin(),min_element(v.begin(),v.end())); //position of smallest no
        int k = 0;
         //this condition checks if d is smaller than the smallest no of vector, if yes then the answer is d 
        if(d+'0' < sml){
            int p  = d;
            for(long int i = 0; i < l1; i++)
                cout << p;
            cout << endl;
        }
        else{
            for(int i = min_pos; i < l1; i++){
                if(v[i] >= sml && v[i] <= d+'0'){
                    c.push_back(v[i]-'0');
                }
            } //all digits starting from smallest digit is checked if it is >= or <= to d. if yes then it is put in the final vector
            int si = c.size();
            for(int i = si; i < l1; i++){
                c.push_back(d);
            } //this is to fill the remaining places with d
            k=0;
            while(si--){
                if(c[k+1] < c[k]){
                   c.erase(c.begin() + k);
                   c.push_back(d);
                } //this is to make sure all digits are sorted. if not then it is removed and a d is inserted
                else k++;
                if(k >= c.size()-1)
                    break;
            }
            for(int i = 0; i < c.size(); i++){
                cout << c[i];
            }
            cout << endl;
        }
    }
}


I don't understand why am I getting WA in this

*comments added
Last edited on
lets read a number, insert it on a stream so we can extract it as a string and then convert it to a vector of chars...

explain your approach, comment your code and use some sane variable names, perhaps then I'll take a look.

here's a testcase
1
1 9
answer is 1, you give 9
Last edited on
closed account (STD8C542)
@ne555

i've added comments and the test case that you gave works fine for me. its giving me 1 not 9
that's because you are invoking undefined behaviour by accessing `c' out of bounds.
if(c[k+1] < c[k])
there was only one digit, but you are trying to access c[1] that doesn't exist

> this is to make sure all digits are sorted
bubble sort in just one pass... doesn't seem likely
1
1342 9
1329
Topic archived. No new replies allowed.