这是我们校赛的一道题,给一个字符串,判断这是字符串描绘的是不是一个堆,并不难,只是一个简单的模拟,但是也稍微有点麻烦,最起码我的方法代码量比较大,主要用栈做一个父亲与儿子的位置匹配,匹配的方法应该有很多.然后在读入的时候注意数字的读入方法,我一开始只读入了一个数导致出错,后来才改对的

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<stack>
  6. using namespace std;
  7. #define INF 0x3f3f3f3f
  8. struct NODE
  9. {
  10. int num;
  11. int lc,rc;
  12. };
  13. NODE node[];
  14. int match[],tot,len;
  15. char str[];
  16. bool is_a_int(char a);
  17. bool is_a_father(int id,int number)
  18. {
  19. node[tot].num = number;
  20. if(str[id+] == ':')
  21. {
  22. return true;
  23. }
  24. else
  25. {
  26. node[tot].lc = node[tot].rc = INF;
  27. return false;
  28. }
  29. }
  30. void find_child(int id)
  31. {
  32. int start,length,now,end,cnt,number;
  33. if(str[id+] == '[')
  34. {
  35. start = id+,length = ,now = start,end;
  36. while(is_a_int(str[now]) && is_a_int(str[now+]))
  37. {
  38. length++;
  39. now++;
  40. }
  41. cnt = ,number = ;
  42. end = start + length;
  43. while(cnt <= length)
  44. {
  45. number += pow(,cnt) * (str[end-cnt] - '');
  46. cnt++;
  47. }
  48. node[tot].lc = number;
  49. }
  50. else if(str[id+] == '(')
  51. {
  52. if(!is_a_int(str[id+]))
  53. node[tot].lc = INF;
  54. else
  55. {
  56. start = id+,length = ,now = start,end;
  57. while(is_a_int(str[now]) && is_a_int(str[now+]))
  58. {
  59. length++;
  60. now++;
  61. }
  62. cnt = ,number = ;
  63. end = start + length;
  64. while(cnt <= length)
  65. {
  66. number += pow(,cnt) * (str[end-cnt] - '');
  67. cnt++;
  68. }
  69. node[tot].lc = number;
  70. }
  71. }
  72. int douhao_id = match[id+];
  73. if(str[douhao_id+] == '(' && str[douhao_id+] == ')')
  74. node[tot].rc = INF;
  75. else if(is_a_int(str[douhao_id+]))
  76. {
  77. start = douhao_id+,length = ,now = start,end;
  78. while(is_a_int(str[now]) && is_a_int(str[now+]) )
  79. {
  80. length++;
  81. now++;
  82. }
  83. cnt = ,number = ;
  84. end = start + length;
  85. while(cnt <= length)
  86. {
  87. number += pow(,cnt) * (str[end-cnt] - '');
  88. cnt++;
  89. }
  90. node[tot].rc = number;
  91. }
  92. else if(str[douhao_id+] == '[')
  93. {
  94. start = douhao_id+,length = ,now = start,end;
  95. while(is_a_int(str[now]) && is_a_int(str[now+]) )
  96. {
  97. length++;
  98. now++;
  99. }
  100. cnt = ,number = ;
  101. end = start + length;
  102. while(cnt <= length)
  103. {
  104. number += pow(,cnt) * (str[end-cnt] - '');
  105. cnt++;
  106. }
  107. node[tot].rc = number;
  108. }
  109. }
  110. bool is_a_int(char a)
  111. {
  112. if(a >= '' && a <= '')
  113. return true;
  114. else return false;
  115. }
  116. int main()
  117. {
  118. int t;
  119. cin>>t;
  120. while(t--)
  121. {
  122. cin>>str;
  123. stack<int> s;
  124. while(!s.empty()) s.pop();
  125. int len = strlen(str);
  126. memset(match,,sizeof(match));
  127. for(int i = ; i < len; i++)
  128. {
  129. if(str[i] == ':')
  130. s.push(i);
  131. else if(str[i] == ',')
  132. {
  133. match[s.top()] = i;
  134. s.pop();
  135. }
  136. }
  137. tot = ;
  138. for(int i = ; i < len-; i++)
  139. {
  140. if(is_a_int(str[i]))
  141. {
  142. int start_id = i,length = ;
  143. int now_id = start_id;
  144. while(is_a_int(str[now_id]) && is_a_int(str[now_id + ]) )
  145. {
  146. length++;
  147. now_id++;
  148. }
  149. int end_id = start_id + length;
  150. int cnt = ,number = ;
  151. while(cnt <= length)
  152. {
  153. number += pow(,cnt) * (str[end_id - cnt] - '');
  154. cnt++;
  155. }
  156. ///cout<<number<<endl;
  157. bool flag = is_a_father(end_id,number);
  158. if(flag)
  159. {
  160. find_child(end_id);
  161. }
  162. tot++;
  163. i += length;
  164. }
  165. }
  166. /*for(int i = 0; i < tot; i++)
  167. {
  168. cout<<node[i].num<<endl;
  169. cout<<node[i].lc<<" "<<node[i].rc<<endl;
  170. }*/
  171. bool flag1 = true;
  172. for(int i = ; i < tot; i++)
  173. {
  174. if(node[i].lc != INF && node[i].num < node[i].lc)
  175. {
  176. flag1 = false;
  177. break;
  178. }
  179. if(node[i].rc != INF && node[i].num < node[i].rc)
  180. {
  181. flag1 = false;
  182. break;
  183. }
  184. }
  185. bool flag2 = true;
  186. for(int i = ; i < tot; i++)
  187. {
  188. if(node[i].lc != INF && node[i].num > node[i].lc)
  189. {
  190. flag2 = false;
  191. break;
  192. }
  193. if(node[i].rc != -INF && node[i].num > node[i].rc)
  194. {
  195. flag2 = false;
  196. break;
  197. }
  198. }
  199. if(flag1 || flag2)
  200. puts("Yes");
  201. else puts("No");
  202. }
  203. return ;
  204. }

hrbustoj 2283 heap解题报告的更多相关文章

  1. ACM-ICPC 2017 Asia HongKong 解题报告

    ACM-ICPC 2017 Asia HongKong 解题报告 任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKon ...

  2. 北大ACM试题分类+部分解题报告链接

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  3. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  4. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  5. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  6. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  7. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

  8. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  9. 习题:codevs 1035 火车停留解题报告

    本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...

随机推荐

  1. kloxo面板教程-折腾了一天

    ------------------------------------------------------------------------------- 前一晚安装了掉线,不得不重新来,有点慢, ...

  2. %02d

    %d表示打印整型的,%2d表示把整型数据打印最低两位,%02d表示把整型数据打印最低两位,如果不足两位,用0补齐所以打印出来就是02了

  3. office2003

    key:

  4. 使用composer安装laravel

    跟具官方文档说:Laravel utilizes Composer to manage its dependencies. So, before using Laravel, you will nee ...

  5. cssText笔记

    style.cssText 用来获取/设置元素的样式 设置: <div id= "a" style= "background: red;"> doc ...

  6. C# 引用参数

    最近经常和同事讨论引用参数的问题,为了搞清楚,查了些资料,其中CLR via C#中讲的比较清楚,整理了下 ----摘自(CLR via C#) 在默认情况下,CLR假设所有的方法参数都是按值传递的. ...

  7. java vector 和ArrayList的区别

    相同点: 1.都是使用数组存储数据 不同点: 1.Vector是显示安全的,ArrayList是线程不安全的 Vector部分代码: public synchronized boolean add(E ...

  8. UIImageView 的contentMode属性 浅析

    UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...

  9. partial 函数

    函数的partial应用 函数在执行时,要带上所有必要的参数进行调用.但是,有时参数可以在函数被调用之前提前获知.这种情况下,一个函数有一个或多个参数预先就能用上,以便函数能用更少的参数进行调用. 例 ...

  10. c语言scanf详解

    函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...