hrbustoj 2283 heap解题报告
这是我们校赛的一道题,给一个字符串,判断这是字符串描绘的是不是一个堆,并不难,只是一个简单的模拟,但是也稍微有点麻烦,最起码我的方法代码量比较大,主要用栈做一个父亲与儿子的位置匹配,匹配的方法应该有很多.然后在读入的时候注意数字的读入方法,我一开始只读入了一个数导致出错,后来才改对的
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<stack>
- using namespace std;
- #define INF 0x3f3f3f3f
- struct NODE
- {
- int num;
- int lc,rc;
- };
- NODE node[];
- int match[],tot,len;
- char str[];
- bool is_a_int(char a);
- bool is_a_father(int id,int number)
- {
- node[tot].num = number;
- if(str[id+] == ':')
- {
- return true;
- }
- else
- {
- node[tot].lc = node[tot].rc = INF;
- return false;
- }
- }
- void find_child(int id)
- {
- int start,length,now,end,cnt,number;
- if(str[id+] == '[')
- {
- start = id+,length = ,now = start,end;
- while(is_a_int(str[now]) && is_a_int(str[now+]))
- {
- length++;
- now++;
- }
- cnt = ,number = ;
- end = start + length;
- while(cnt <= length)
- {
- number += pow(,cnt) * (str[end-cnt] - '');
- cnt++;
- }
- node[tot].lc = number;
- }
- else if(str[id+] == '(')
- {
- if(!is_a_int(str[id+]))
- node[tot].lc = INF;
- else
- {
- start = id+,length = ,now = start,end;
- while(is_a_int(str[now]) && is_a_int(str[now+]))
- {
- length++;
- now++;
- }
- cnt = ,number = ;
- end = start + length;
- while(cnt <= length)
- {
- number += pow(,cnt) * (str[end-cnt] - '');
- cnt++;
- }
- node[tot].lc = number;
- }
- }
- int douhao_id = match[id+];
- if(str[douhao_id+] == '(' && str[douhao_id+] == ')')
- node[tot].rc = INF;
- else if(is_a_int(str[douhao_id+]))
- {
- start = douhao_id+,length = ,now = start,end;
- while(is_a_int(str[now]) && is_a_int(str[now+]) )
- {
- length++;
- now++;
- }
- cnt = ,number = ;
- end = start + length;
- while(cnt <= length)
- {
- number += pow(,cnt) * (str[end-cnt] - '');
- cnt++;
- }
- node[tot].rc = number;
- }
- else if(str[douhao_id+] == '[')
- {
- start = douhao_id+,length = ,now = start,end;
- while(is_a_int(str[now]) && is_a_int(str[now+]) )
- {
- length++;
- now++;
- }
- cnt = ,number = ;
- end = start + length;
- while(cnt <= length)
- {
- number += pow(,cnt) * (str[end-cnt] - '');
- cnt++;
- }
- node[tot].rc = number;
- }
- }
- bool is_a_int(char a)
- {
- if(a >= '' && a <= '')
- return true;
- else return false;
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- cin>>str;
- stack<int> s;
- while(!s.empty()) s.pop();
- int len = strlen(str);
- memset(match,,sizeof(match));
- for(int i = ; i < len; i++)
- {
- if(str[i] == ':')
- s.push(i);
- else if(str[i] == ',')
- {
- match[s.top()] = i;
- s.pop();
- }
- }
- tot = ;
- for(int i = ; i < len-; i++)
- {
- if(is_a_int(str[i]))
- {
- int start_id = i,length = ;
- int now_id = start_id;
- while(is_a_int(str[now_id]) && is_a_int(str[now_id + ]) )
- {
- length++;
- now_id++;
- }
- int end_id = start_id + length;
- int cnt = ,number = ;
- while(cnt <= length)
- {
- number += pow(,cnt) * (str[end_id - cnt] - '');
- cnt++;
- }
- ///cout<<number<<endl;
- bool flag = is_a_father(end_id,number);
- if(flag)
- {
- find_child(end_id);
- }
- tot++;
- i += length;
- }
- }
- /*for(int i = 0; i < tot; i++)
- {
- cout<<node[i].num<<endl;
- cout<<node[i].lc<<" "<<node[i].rc<<endl;
- }*/
- bool flag1 = true;
- for(int i = ; i < tot; i++)
- {
- if(node[i].lc != INF && node[i].num < node[i].lc)
- {
- flag1 = false;
- break;
- }
- if(node[i].rc != INF && node[i].num < node[i].rc)
- {
- flag1 = false;
- break;
- }
- }
- bool flag2 = true;
- for(int i = ; i < tot; i++)
- {
- if(node[i].lc != INF && node[i].num > node[i].lc)
- {
- flag2 = false;
- break;
- }
- if(node[i].rc != -INF && node[i].num > node[i].rc)
- {
- flag2 = false;
- break;
- }
- }
- if(flag1 || flag2)
- puts("Yes");
- else puts("No");
- }
- return ;
- }
hrbustoj 2283 heap解题报告的更多相关文章
- ACM-ICPC 2017 Asia HongKong 解题报告
ACM-ICPC 2017 Asia HongKong 解题报告 任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKon ...
- 北大ACM试题分类+部分解题报告链接
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- CH Round #56 - 国庆节欢乐赛解题报告
最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...
- 二模13day1解题报告
二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...
- BZOJ 1051 最受欢迎的牛 解题报告
题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4438 Solved: 2353[S ...
- 习题:codevs 2822 爱在心中 解题报告
这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...
- 习题:codevs 1035 火车停留解题报告
本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...
随机推荐
- kloxo面板教程-折腾了一天
------------------------------------------------------------------------------- 前一晚安装了掉线,不得不重新来,有点慢, ...
- %02d
%d表示打印整型的,%2d表示把整型数据打印最低两位,%02d表示把整型数据打印最低两位,如果不足两位,用0补齐所以打印出来就是02了
- office2003
key:
- 使用composer安装laravel
跟具官方文档说:Laravel utilizes Composer to manage its dependencies. So, before using Laravel, you will nee ...
- cssText笔记
style.cssText 用来获取/设置元素的样式 设置: <div id= "a" style= "background: red;"> doc ...
- C# 引用参数
最近经常和同事讨论引用参数的问题,为了搞清楚,查了些资料,其中CLR via C#中讲的比较清楚,整理了下 ----摘自(CLR via C#) 在默认情况下,CLR假设所有的方法参数都是按值传递的. ...
- java vector 和ArrayList的区别
相同点: 1.都是使用数组存储数据 不同点: 1.Vector是显示安全的,ArrayList是线程不安全的 Vector部分代码: public synchronized boolean add(E ...
- UIImageView 的contentMode属性 浅析
UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...
- partial 函数
函数的partial应用 函数在执行时,要带上所有必要的参数进行调用.但是,有时参数可以在函数被调用之前提前获知.这种情况下,一个函数有一个或多个参数预先就能用上,以便函数能用更少的参数进行调用. 例 ...
- c语言scanf详解
函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...