2013-10-28 110 views
0

我正在特林解决这个http://www.spoj.com/problems/LEXISORT/问题 它在Visual Studio编译器和IDEone工作的罚款也是,但是当我在SPOJ编译运行它越来越SEGSIGV错误请帮我找错误在我的代码(分段错误)

这里我的代码去

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 

char *getString(); 
void lexisort(char **str,int num); 
void countsort(char **str,int i,int num); 
int main() 
{ 
    int num_test; 
    int num_strings; 
    char **str; 
    int i,j; 
    scanf("%d",&num_test); 
    for(i=0;i<num_test;i++) 
    { 
      scanf("%d",&num_strings); 
     str=(char **)malloc(sizeof(char *)*num_strings); 
     for(j=0;j<num_strings;j++) 
     { 
      str[j]=(char *)malloc(sizeof(char)*11); 
      scanf("%s",str[j]); 
      } 
     lexisort(str,num_strings); 
     for(j=0;j<num_strings;j++) 
     { 
      printf("%s\n",str[j]); 
      free(str[j]); 
     } 
     free(str); 
    } 
    return 0; 
} 


void lexisort(char **str,int num) 
{ 
    int i; 
    for(i=9;i>=0;i--) 
    { 
     countsort(str,i,num); 
    } 
} 
void countsort(char **str,int i,int num) 
{ 

    int buff[52]={0,0},k,x; 
    char **temp=(char **)malloc(sizeof(char *)*num); 
    for(k=0;k<52;k++) 
    { 
     buff[k]=0; 
    } 
    for(k=0;k<num;k++) 
    { 
     if(str[k][i]>='A' && str[k][i]<='Z') 
     { 
      buff[(str[k][i]-'A')]++; 
     } 
     else 
     { 
      buff[26+(str[k][i]-'a')]++; 
     } 
    } 
    for(k=1;k<52;k++) 
    { 
     buff[k]=buff[k]+buff[k-1]; 
    } 
    for(k=num-1;k>=0;k--) 
    { 
     if(str[k][i]>='A' && str[k][i]<='Z') 
     { 

      x=buff[(str[k][i]-'A')]; 
      temp[x-1]=str[k]; 
      buff[(str[k][i]-'A')]--; 
     } 
     else 
     { 
      x=buff[26+(str[k][i]-'a')]; 
       temp[x-1]=str[k]; 
      buff[26+(str[k][i]-'a')]--; 
       } 
    } 
    for(k=0;k<num;k++) 
    { 
     str[k]=temp[k]; 
    } 
    free(temp); 
} 
+1

它在哪里段错误?什么线?你的调试器告诉你什么?另外,尝试初始化您的变量。如果你整理了缩进的话,它也会有很大的帮助 - 我要独眼专注。 – mjs

回答

0

一般来说,这些在线法官的程序举个例子输入(在这种情况下,输入似乎很好地工作),但也使用了一套更难隐藏的输入。

在这种情况下,如果输入字符串中有空格,会发生什么情况?例如,输入:

1 
2 
hello orld 
whateverss 

在这种情况下,你的scanf("%s",str[j]);将无法​​正确读取输入线。我建议切换到getline样式界面,而不是scanf样式界面。

+0

我不这么认为一个字不包含空格 –