Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя r0n1n

Автор r0n1n, история, 5 лет назад, По-английски

I am facing run time error . The error comes for the test case 2 33 1 1 1 1 1 1 1 1 1

In my code when I am comparing the value of the element in the string and the value at the index of the array.53606476 //MEHNAT

include<bits/stdc++.h>

using namespace std;

define tr(c, it) \

for(auto it = c.begin(); it != c.end(); it++)

define ll long long

//const int MAX=1e3 + 5;

bool flag=0;

int main(){ int n; string s=""; cin>>n>>s; int a[n]; for(int i=1;i<10;i++){ cin>>a[i]; }

stack <pair<int,int> > st;

pair <int,int> p;
p.first=0;
p.second=0;
int b;
for(int i=n-1;i>=0;i--){
    b=((int) s[i]) - 48;
    if(b<a[b] && !flag){     //Facing the error here for a[b].
        p.second=i;
        flag=1;
    }
    if(b>=a[b] && flag){
        p.first=i+1;
        st.push(p);
        flag=0;
    }
    if(b<a[b] && flag && i==0){
        p.first=0;
        st.push(p);
        flag=0;
    }
}

/*
for(int i=0;st.size();i++){
    q=st.top();
    st.pop();
    cout<<q.first<<" "<<q.second<<endl;
}*/

if(st.empty()){
    cout<<s;
}
else{
    pair <int,int> q;
    q=st.top();
    for(int i=q.first;i<=q.second;i++){
        int b=((int) s[i]) - 48;
        s[i]=a[b] + 48;
    }
    cout<<s;
}


return 0;

}

/* check for corner cases(n == 1?) see the constraint read the highlighted text again */

  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +25 Проголосовать: не нравится
  1. Please remove your code from the blog post. I think that the link is sufficient.
  2. Since you have the test case, can't you run your code through a debugger and check for where you have illegal values/memory accesses?
  3. You can also insert some arbitrary print statements into your code and run it at the custom invocation.

Please don't be lazy to debug your own code.

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Here is the problem:

cin>>n>>s;
int a[n];
for(int i=1;i<10;i++){
   cin>>a[i];
}

The size of the array is n but you are trying to store 10 elements in it.