SPOJ - Runtime error SIGSEGV

谁说我不能喝 提交于 2021-02-05 08:33:22

问题


Following is the implementation of infix to postfix conversion, it is working fine on my computer, but as I am submitting in on SPOJ it is giving me Runtime error SIGSEGV, I am new to competitive programming and I am unable to handle such type of errors.

#include <iostream>
#include <stack>
#include<string.h>
#include<ctype.h>
using namespace std;
int prec(char ch){
    switch(ch){
        case '^' : return 3;
                    break;
        case '*':
        case '/': return 2;
                break;
        case '+':
        case '-': return 1;
                break;
        default: return -1;
    }
}
void pti(char a[]){
    stack<int> post;
    int k = -1;
    for(int i = 0;i<strlen(a);i++){
        if(isalnum(a[i]))
            a[++k] = a[i];
        else if(a[i] == '(')
            post.push(a[i]);
        else if(a[i] == ')'){
            while(!post.empty() && post.top()!= '('){
                a[++k] = post.top();
                post.pop();
            }
            post.pop();
        }
        else {
            while(!post.empty() && prec(a[i]) <= prec(post.top())){
                a[++k] = post.top();
                post.pop();
            }
            post.push(a[i]);
        }
    }
    while(!post.empty()){
        a[++k] = post.top();
        post.pop();
    }
    a[++k] = '\0';
    cout<<a<<endl;
}
int main()
{
   int t;
   cin>>t;
   for(int i = 0;i<t;i++){
        char a[100];
        cin>>a;
        pti(a);
   }
}

回答1:


You just need to make the input array longer, e.g. a size of 1000 gets AC.

The SIGSEGV signal means a segmentation fault occured, which basically means you accessed memory that doesn't belong to you.



来源:https://stackoverflow.com/questions/40488174/spoj-runtime-error-sigsegv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!