程序执行随着I / P大小增加超过5而变化

这是个问题。

对于两个字符串A和B,我们将字符串的相似性定义为两个字符串共有的最长前缀的长度。 例如,字符串“abc”和“abd”的相似性是2,而字符串“aaa”和“aaab”的相似性是3.计算字符串S与其后缀中的每一个的相似度之和。

输入:第一行包含测试用例T的数量。下一个T行中的每一行都包含一个字符串。

输出:输出T行包含相应测试用例的答案。

约束: 1 <= T <= 10每个字符串的长度最多为100000,仅包含小写字符。

样本输入: 2 ababaa aa

样本输出: 11 3

说明:对于第一种情况,字符串的后缀是“ababaa”,“babaa”,“abaa”,“baa”,“aa”和“a”。 每个字符串与字符串“ababaa”的相似性分别为6,0,3,0,1,1。 因此答案是6 + 0 + 3 + 0 + 1 + 1 = 11。

我面临的问题:它适用于小于5的测试用例。对于5和超过5,第一个字符串的输出打印为0.对于调试,我使用字符变量k来查找指向的值指针。 在计算第一个字符串时,k具有值-54,-56和其他值。 除第一个字符串外,它对其他字符串正常工作。

我甚至试过打印第一个字符串。 正在打印一些垃圾值。 但它正在为测试用例正确打印<5。我已经给出了下面的代码。 请帮我。

#include #include #include int main() { int test_cases,i,j,*count; char k; //for testing purpose to determine the character at each iteration scanf("%d",&test_cases); count = calloc(test_cases,sizeof(int)); char **strings, *initial_ptr, *current_ptr, *start_ptr; strings = malloc(test_cases); for(i=0;i<test_cases;i++) { strings[i] = malloc(100000); scanf("%s",strings[i]); } initial_ptr = start_ptr = *strings; current_ptr = *strings; //testing printf("This is the first string: "); puts(strings[0]); int temp_count=0; for(i=0;i<test_cases;i++) { current_ptr = initial_ptr = start_ptr = *(strings+i); temp_count=0; for(j=0;j= 'a') && (*current_ptr <= 'z')) { if(*current_ptr == *initial_ptr) { temp_count++; current_ptr++; initial_ptr++; } else { start_ptr++; current_ptr = start_ptr ; initial_ptr = *(strings+i) ; } } current_ptr = start_ptr; count[i]=temp_count; } } for(i=0;i<test_cases;i++) { printf("\n%d",count[i]); } return 0; } 

 count = (int *)calloc(test_cases*sizeof(int),0); 

没有意义。 calloc的第二个参数是您要分配的元素的大小。 该电话应为:

 count = calloc(test_cases, sizeof(int)); 

这个也是错的:

 strings = (char **) malloc(test_cases); 

应该:

 strings = malloc(test_cases*sizeof(char*)); 

这个:

 printf("This is the first string: "); puts(strings[1]); 

是误导:它打印第二个字符串。