程序执行不同的I / P尺寸超过5用C增加 [英] Program execution varies as the I/P size increases beyond 5 in C

查看:126
本文介绍了程序执行不同的I / P尺寸超过5用C增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是个问题。

有关两个串A和B,我们定义了字符串的相似是通用的字符串最长preFIX的长度。例如,字符串ABC和ABD的相似性为2,而字符串AAA和AAAB的相似性为3。
计算一个字符串的S相似的总和与每个它的后缀。

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

输出:
包含相应的测试情况下,答案输出T线。

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

输入示例:
2
ababaa
AA

示例输出:
11
3

说明:
对于第一种情况下,字符串的后缀ababaa,巴巴,abaa中,咩,AA和一个。每个分别这些字符串以字符串ababaa的相似之处是6,0,3,0,1,1。因而答案是6 + 0 + 3 + 0 + 1 + 1 = 11

,我面对的问题:
这是测试用例小于5。5及以后5​​,输出的第一个字符串打印为0。调试工作正常,我使用的字符变量k找到由指针指向的值。 K为第一个字符串的时候计算过的值-54,-56和其他值。这是正常工作的其他字符串除了第一个。

我甚至尝试打印的第一个字符串。正在打印一些垃圾值。但它是测试用例&LT打印正确; 5.我已经给下面的code。请帮我。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
诠释的main()
{
    INT test_cases,I,J,*计数;
    字符K表; //用于测试目的,以确定在每次迭代的字符
    scanf函数(%d个,&安培; test_cases);
    数=释放calloc(test_cases,sizeof的(INT));
    焦炭**字符串,* initial_ptr,* current_ptr,* start_ptr;
    字符串=的malloc(test_cases);
    对于(i = 0; I< test_cases;我++)
    {
       字符串[我] =的malloc(100000);
       scanf函数(%S,字符串[我]);
    }
    initial_ptr = start_ptr = *串;
    current_ptr = *串;    //测试
    的printf(这是第一个字符串:);
    看跌期权(串[0]);    INT temp_count = 0;
    对于(i = 0; I< test_cases;我++)
    {
       current_ptr = initial_ptr = start_ptr = *(字符串+ I);
       temp_count = 0;
       为(J = 0; J<的strlen(字符串[我]); J ++)
       {
         K = * current_ptr;
         而((* current_ptr)及及(* current_ptr> ='一')及及(* current_ptr&下; ='Z'))
         {
           如果(* current_ptr == * initial_ptr)
           {             temp_count ++;
             current_ptr ++;
             initial_ptr ++;
           }
           其他
           {
             start_ptr ++;
             current_ptr = start_ptr;
             initial_ptr = *(字符串+ I);
           }
         }
         current_ptr = start_ptr;
         算上[I] = temp_count;       }
    }
    对于(i = 0; I< test_cases;我++)
    {
     的printf(\\ N%D,算[I]);
    }
    返回0;}


解决方案

 计数=(INT *)释放calloc(test_cases *的sizeof(INT),0);

是没有意义的。在第二个参数释放calloc 是你分配单元的大小。这一呼吁应该阅读:

 计数=释放calloc(test_cases,sizeof的(INT));

这一个是错的太多:

 字符串=(字符**)的malloc(test_cases);

应该是:

 字符串=的malloc(test_cases * sizeof的(字符*));

 的printf(这是第一个字符串:);
看跌期权(串[1]);

是一种误导:它打印的第二的字符串

This is the question.

For two strings A and B, we define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of strings "abc" and "abd" is 2, while the similarity of strings "aaa" and "aaab" is 3. Calculate the sum of similarities of a string S with each of it's suffixes.

Input: The first line contains the number of test cases T. Each of the next T lines contains a string each.

Output: Output T lines containing the answer for the corresponding test case.

Constraints: 1 <= T <= 10 The length of each string is at most 100000 and contains only lower case characters.

Sample Input: 2 ababaa aa

Sample Output: 11 3

Explanation: For the first case, the suffixes of the string are "ababaa", "babaa", "abaa", "baa", "aa" and "a". The similarities of each of these strings with the string "ababaa" are 6,0,3,0,1,1 respectively. Thus the answer is 6 + 0 + 3 + 0 + 1 + 1 = 11.

The problem that I am facing : It is working correctly for test cases less than 5. For 5 and beyond 5, the output for the first string is printed as 0. For debugging, I used a character variable k to find the value pointed by the pointers. k had the values -54, -56 and other values when calculating for the first string. It is working correctly for other strings except the first one.

I even tried printing the first string. Some garbage value is being printed. But it is printing correctly for test cases < 5. I have given the code below. Please help me.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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<strlen(strings[i]);j++)
       {
         k = *current_ptr;
         while((*current_ptr) && (*current_ptr >= '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);

makes no sense. The second parameter in calloc is the size of the elements you're allocating. That call should read:

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

This one is wrong too:

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

should be:

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

This:

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

is misleading: it prints the second string.

这篇关于程序执行不同的I / P尺寸超过5用C增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆