zoukankan      html  css  js  c++  java
  • 第七届 山东省ACM Memory Leak(模拟 待整理)

    Memory Leak

    Time Limit: 2000MS Memory Limit: 131072KB

    Problem Description

    Memory Leak is a well-known kind of bug in C/C++. When a string is longer than expected, it will visit the memory of next array which will cause the issue and leak some information. You can see a simple example bellow:

    As we see, if the length of the input string is equal to or larger than the limit length of array, the extra part won’t be stored and the information of next array will be leaked out while outputting. The output will stop until a ‘’ character (the symbol of end of a string) is found. In this problem, there will never be unexpected end of the program and the last array won’t leak other information.

    Source code given as follow:

    Now a simpler source code will be given. What will the output be?

    Input

     Multiple test cases, the first line is an integer T (T <= 20), indicating the number of test cases.

    The first line of each test case contains a non-empty string, the definition of strings, formatted as “char s1[s1_len], s2[s2_len]...;”. “char ” is the type of the array which will never change. s1, s2... is the name of the array. s1_len, s2_len... is the length limit. If nothing goes wrong, the array should be able to store the input string and a ‘’. The definitions of different arrays will be separated by a comma and a space. The length limits are positive and the Sum of length limit will be less than 10000.

    Then, there will be several lines of string which consists two or three parts.

    The first part is “gets” or “cout”, the second part will a string s, indicates the name of the array. s will contains only lower case letters and number digits and start with letters. s will be different in one case. If the first part is “gets”, then there will be the third part, a string which should be input into array s, the length of input string will be less than 1000 and contains only visible ASCII characters. “gets” operation will rewrite the array no matter what was in the array before and add a ‘’ after the string. Different parts are separated by a space.

    Case ends with “return 0;”

    The whole input file is less than 10MB.

    Output

     For each “cout”, you should output a line contains what will actually output, which means you should consider the issue of memory leak. If the requested array is empty, you should output an empty line.

    Example Input

    3char a[5], b[5], c[5];gets a C++gets b Hello, world!gets c guyscout acout bcout creturn 0;char a[5];cout agets a 233gets a 2333cout agets a 12345cout areturn 0;char str1[7], str2[2], str3[5];gets str1 welcomegets str2 togets str3 Shandongcout str1return 0;

    Example Output

    C++Helloguysguys233312345welcometoShand

    Hint

     

    Author

      “浪潮杯”山东省第七届ACM大学生程序设计竞赛

    #include <bits/stdc++.h>
    using namespace std;
    #define MAX 11111
    struct node
    {
       int l,r;
    }e[MAX];
    int top;
    char s[MAX];
    char c[MAX][1111];
    char op[MAX];
    
    int main()
    {
        int T;
        scanf("%d",&T);
    
        while(T--)
        {
            memset(s,0,sizeof(s));
            int pos = 0;
            top = 0;
            scanf("%s",op);
    
            while(1)
            {
                scanf("%s",op);
                int len = strlen(op);
                int num = 0,i = 0;
                
                for(i = 0;i < len; i++)
                {
                    if(op[i] != '[')
                        c[top][i] = op[i];
                    else break;
                }
                c[top][i] = '';
                for(i++ ;i < len; i++)
                {
                    if(op[i] == ']') break;
                    num = num * 10 + op[i] -'0';
                }
                e[top].l = pos;
                e[top].r = pos+num;
                pos+=num;
                top++;
                char ss = getchar();
                if(ss == '
    ') break;
            }
            while(1)
            {
                scanf("%s",op);
                if(op[0] == 'r')
                {
                    scanf("%s",op);
                    break;
                }
                if(op[0] == 'c') 
                {
                    scanf("%s", op);
                    for(int i = 0;i < top; i++)
                    {
                        if(strcmp(op,c[i]) == 0)
                        {
                            for(int j = e[i].l ; j < pos; j++)
                            {
                                if(s[j] == '') break;
                                printf("%c",s[j]);
                            }
                            printf("
    ");
                            break;
                        }
                    }
                }
                else if(op[0] == 'g')
                {
                    scanf("%s",op);
                    for(int i = 0 ;i < top; i++)
                    {
                        if(strcmp(op,c[i]) == 0)
                        {
                            gets(op);
                            int len = strlen(op);
                            int k = 1,j;
                            for( j = e[i].l ; j < e[i].r && k < len; j++, k++)
                                s[j] = op[k];
                            if(j < e[i].r)
                                s[j] = '';
                            break;
                        }
                    }
                }
            }
        }
        return 0;
    }



  • 相关阅读:
    列"xx"不在表Table中
    asp.net中自定义验证控件
    ASP.NET母版与内容页相对路径的问题
    html点小图看大图最快捷的方法
    ThinkCMF的跳转303 404等页面的方法
    关于ThinkCMF自带插件上传不了图片的解决方法
    js中百分比运算,大型数据会算错
    数据库价格汇总查询的方法
    信息资源5
    操作系统概论
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717880.html
Copyright © 2011-2022 走看看