AmoDraper's blog

By AmoDraper, history, 2 years ago, In English

Hi Codeforces Community! For the problem 'Decode String' I am getting a strange error, for the sample test case 8 i.e. :- 5 11111 I get am getting a wrong answer, mainly instead of 'aaaaa' I get 'aaaa:' this does not happen when you run this test case indivisually, but when taking all 9 test cases only this one is for some reason outputing wrong answer

Here's my code :-

#include<bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while (t--) {
        int n;
        cin>>n;
        string arr;
        cin>>arr;
        for(int i = 0 ; i < n ; i++){
            char temp = 48;
            int sum = arr[i];
            if((arr[i+2]) == '0' && (arr[i+3]) != '0') {
                sum = sum*10 + (arr[++i])+32;
                i++;
            }
            temp+=sum;
            cout<<temp;
        }
        cout<<endl;
    }
    return 0;
}

Your Help is Greatly Appreciated. :-)

  • Vote: I like it
  • -5
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Do give problem link next time, had to guess for where the problem was.

The problem was that your string is only length $$$N$$$, but you are accessing arr[i+2] and arr[i+3], as well as arr[++i] would also get it out-of-bounds. There's no telling what content is in that memory slot when you access it.