Memory Leak

Time Limit: 2000MS Memory limit: 131072K

题目描述

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 ‘\0’ 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?

输入

 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 ‘\0’. 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 ‘\0’
after the string. Different parts are separated by a space.

Case ends with “return 0;”

The whole input file is less than 10MB.

输出

 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.

示例输入

  1. 3
  1. char a[5], b[5], c[5];
  1. gets a C++
  1. gets b Hello, world!
  1. gets c guys
  1. cout a
  1. cout b
  1. cout c
  1. return 0;
  1. char a[5];
  1. cout a
  1. gets a 233
  1. gets a 2333
  1. cout a
  1. gets a 12345
  1. cout a
  1. return 0;
  1. char str1[7], str2[2], str3[5];
  1. gets str1 welcome
  1. gets str2 to
  1. gets str3 Shandong
  1. cout str1
  1. return 0;

示例输出

  1. C++
  1. Helloguys
  1. guys
  1. 2333
  1. 12345
  1. welcometoShand

来源

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

题意

一道不算很难的模拟题吧!输入的是字符串,模拟操作,定义数组,输入到数组,然后输出,其实呢!主要考察的是内存泄漏的问题,也就是一个数组的最后没有‘\0’,然后输出的时候会访问到下一段内存空间中!

AC代码:(别人的)
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4. {
  5. int l;
  6. int r;
  7. } e[11111];
  8. int top;
  9. char s[11111];
  10. char c[11111][1111];
  11. char op[11111];
  12. int main()
  13. {
  14. int T;
  15. scanf("%d",&T);
  16. while(T--)
  17. {
  18. memset(s,0,sizeof(s));
  19. int pos = 0;
  20. top = 0;
  21. scanf("%s",op);
  22. while(true)
  23. {
  24. scanf("%s",op);
  25. int len = strlen(op);
  26. int num = 0,i = 0;
  27. for(i = 0; i < len; i++)
  28. {
  29. if(op[i] != '[')c[top][i] = op[i];
  30. else break;
  31. }
  32. c[top][i] = '\0';
  33. for(i++ ; i < len; i++)
  34. {
  35. if(op[i] == ']') break;
  36. num = num * 10 + op[i] -'0';
  37. }
  38. e[top].l = pos;
  39. e[top].r = pos+num;
  40. pos+=num;
  41. top++;
  42. char ss = getchar();
  43. if(ss == '\n') break;
  44. }
  45. while(1)
  46. {
  47. scanf("%s",op);
  48. if(op[0] == 'r')
  49. {
  50. scanf("%s",op);
  51. break;
  52. }
  53. if(op[0] == 'c')
  54. {
  55. scanf("%s", op);
  56. for(int i = 0; i < top; i++)
  57. {
  58. if(strcmp(op,c[i]) == 0)
  59. {
  60. for(int j = e[i].l ; j < pos; j++)
  61. {
  62. if(s[j] == '\0') break;
  63. printf("%c",s[j]);
  64. }
  65. printf("\n");
  66. break;
  67. }
  68. }
  69. }
  70. else if(op[0] == 'g')
  71. {
  72. scanf("%s",op);
  73. for(int i = 0 ; i < top; i++)
  74. {
  75. if(strcmp(op,c[i]) == 0)
  76. {
  77. gets(op);
  78. int len = strlen(op);
  79. int k = 1,j;
  80. for( j = e[i].l ; j < e[i].r && k < len; j++, k++)
  81. s[j] = op[k];
  82. if(j < e[i].r)
  83. s[j] = '\0';
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. return 0;
  91. }

TLE代码:(自己的)
  1. #include"stdio.h"
  2. #include"string.h"
  3. #include<iostream>
  4. using namespace std;
  5. #include<map>
  6. char data[1000005];
  7. map<string,int>k;
  8. map<string,int>ss;
  9. int main()
  10. {
  11. int N;
  12. cin>>N;
  13. while(N--)
  14. {
  15. k.clear();
  16. ss.clear();
  17. memset(data,0,sizeof(data));
  18. string start;
  19. int ci=0;
  20. while(cin>>start)
  21. {
  22. if(start=="char")
  23. {
  24. char c[1005]= {0};
  25. int size=0;
  26. char jud=0;
  27. while(~scanf("%*[ ]%[^[][%d]%c",c,&size,&jud))
  28. {
  29. k[c]=ci;
  30. ss[c]=size;
  31. ci+=size;
  32. if(jud==';')break;
  33. }
  34. }
  35. else if(start=="gets")
  36. {
  37. string value;
  38. cin>>value;
  39. scanf("%*[ ]");
  40. gets(data+k[value]);
  41. data[k[value]+ss[value]]=0;
  42. memset(data+k[value]+ss[value],0,sizeof(data+k[value]+ss[value]));
  43. for(int i=k[value]+ss[value]; i<=1000005; i++)
  44. data[i]=0;
  45. // cout<<"/////////////////////////////////////"<<endl;
  46. // for(int i=0;i<20;i++)
  47. // printf(i!=19?"%c":"%c\n",data[i]);
  48. }
  49. else if(start=="cout")
  50. {
  51. string va;
  52. cin>>va;
  53. if(ss[va])puts(data+k[va]);
  54. // for(int i=k[va];i<k[va]+(int)strlen(data+k[va])&&i<ci;i++)
  55. // putchar(data[i]);
  56. // printf("\n");
  57. }
  58. else if(start=="return")
  59. {
  60. fflush(stdin);
  61. break;
  62. }
  63. }
  64. }
  65. return 0;
  66. }

伤心(;′⌒`)~

山东省第七届ACM省赛------Memory Leak的更多相关文章

  1. 山东省第七届ACM省赛------Reversed Words

    Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...

  2. 山东省第七届ACM省赛------Triple Nim

    Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...

  3. 山东省第七届ACM省赛------The Binding of Isaac

    The Binding of Isaac Time Limit: 2000MS Memory limit: 65536K 题目描述 Ok, now I will introduce this game ...

  4. 山东省第七届ACM省赛------Fibonacci

    Fibonacci Time Limit: 2000MS Memory limit: 131072K 题目描述 Fibonacci numbers are well-known as follow: ...

  5. 山东省第七届ACM省赛------Julyed

    Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...

  6. 山东省第七届ACM省赛

    ID Title Hint A Julyed 无 B Fibonacci 打表 C Proxy 最短路径 D Swiss-system tournament 归并排序 E The Binding of ...

  7. 山东省第十届ACM省赛参赛后的学期总结

    5.11,5.12两天的济南之旅结束了,我也参加了人生中第一次正式的acm比赛,虽然是以友情队的身份,但是我依旧十分兴奋. 其实一直想写博客来增加自己的能力的,但是一直拖到现在,正赶上老师要求写一份总 ...

  8. 山东理工大学第七届ACM校赛-LCM的个数 分类: 比赛 2015-06-26 10:37 18人阅读 评论(0) 收藏

    LCM的个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于我们来说求两个数的LCM(最小公倍数)是很容易的事,现在我遇到了 ...

  9. 山东理工大学第七届ACM校赛-完美素数 分类: 比赛 2015-06-26 10:36 15人阅读 评论(0) 收藏

    完美素数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 我们定义:如果一个数为素数,且这个数中含有7或3,那么我们称这个数为完美 ...

随机推荐

  1. Magento学习第一课——目录结构介绍

    Magento学习第一课--目录结构介绍 一.Magento为何强大 Magento是在Zend框架基础上建立起来的,这点保证了代码的安全性及稳定性.选择Zend的原因有很多,但是最基本的是因为zen ...

  2. linux命令日常总结

    1.date 显示系统日期2. mkdir xx 创建xx目录 rmdir xx 删除xx目录(空目录) rm -rf xx 删除xx目录(非空目录) 3. vi xx 创建某文件 写入-->e ...

  3. Python之路----------time模块

    时间模块是常用的模块 一.time模块 import time print(time.clock())#返回处理器时间,3.3开始已经屏蔽. print(time.altzone)#返回与UTC时间差 ...

  4. 配置sqlserver端口

    今天写java连接数据库时,出现错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败.错误:“Connection refused: connect.请验证连接属性,并 ...

  5. HDU 4504 威威猫系列故事——篮球梦(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4504 题目大意: 中文都看得懂.不过我是看hint才正确理解什么意思的.开始的时候理解错了. 解题思路: 给定时 ...

  6. Android Studio Jni 环境搭建

    第一步:NDK环境搭建,点击下图红色框区域查看NDK下载和环境配置 安照正常情况是很慢的或者无法下载成功的,这个时候可以去下载NDK压缩包进行解压.下面给出两个下载地址 (1)官网:http://we ...

  7. Java操作Excel: POI不能创建xlsm问题的方法(源自StackOverFlow)

    write to xlsm (Excel 2007) using apache poi POI的下载(记得把其中的jar包全部加到工程里哦)http://mirror.bit.edu.cn/apach ...

  8. DB2 常用命令小结

    . 打开命令行窗口 #db2cmd . 打开控制中心 # db2cmd db2cc . 打开命令编辑器 db2cmd db2ce =====操作数据库命令===== . 启动数据库实例 #db2sta ...

  9. C# socket通信

    最近在研究socket,今天看到很好的一篇关于socket通信的文章,故收藏了,慢慢琢磨. 我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: 1.TCP/IP层次模型 当然这里 ...

  10. 【转】CentOS系统中常用查看日志命令

    来源:http://www.centoscn.com/CentOS/help/2014/0310/2540.html Linux IDE RedHat 防火墙活动 .cat tail -f 日 志 文 ...