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. mysql_索引原理及优化

    思考: 我们知道mysql最好的数据存储量级是百万级别,是的往往在百万级别或者几十万级别就会出现慢查询(我对慢查询的定义是大于1秒),几年前我所在的一个做pos机支付的联机交易的核心系统组,当时就做过 ...

  2. ASP.NET Web API 2基于令牌的身份验证

    基于令牌的认证 我们知道WEB网站的身份验证一般通过session或者cookie完成的,登录成功后客户端发送的任何请求都带上cookie,服务端根据客户端发送来的cookie来识别用户. WEB A ...

  3. JS检测移动端横竖屏

    (function () {                                var supportOrientation = (typeof window.orientation == ...

  4. 转:HAR(HTTP Archive)规范

    HAR(HTTP Archive),是一个用来储存HTTP请求/响应信息的通用文件格式,基于JSON.这个格式的出现可以使HTTP监测工具以一种通用的格式导出所收集的数据,这些数据可以被其他支持HAR ...

  5. 移动端的传统click事件延迟和点透现象

    一.场景描述: 1.A/B两个层上下z轴重叠. 2.上层的A点击后消失或移开.(这一点很重要) 3.B元素本身有默认click事件(如a标签) 或 B绑定了click事件. 在以上情况下,点击A/B重 ...

  6. ipxe引导远程的windows

    使用ipxe解决本地引导远程系统 本地安装的centos7,然后修改grub.cfg来使用ipxe技术引导远程windows,实现双系统 os-->centos7 修改grub.cfg 在文件最 ...

  7. PHP会话管理:cookie和session

    PHP会话管理1.cookie数据存储在浏览器端方便与JavaScript交换数据方便获取用户信息风险-浏览器可能会禁用cookie替代方案-URL参数 2.session数据存储在服务器高效.安全. ...

  8. oracle or语句的坑

    SELECT SUM(tjo.pay_amount) FROM tb_jf_order tjo,tb_jf_gateway_trade_log tjg WHERE tjo.order_id = tjg ...

  9. Docker应用程序容器技术_转

    转自:百度百科 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相 ...

  10. 对bootstrap中confirm alert进行封装

    HTML: <!-- system modal start --> <div id="ycf-alert" class="modal"> ...