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

#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解题报告的更多相关文章

  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. linux脚本Shell之awk详解(二)

    三.printf的使用   print format 生成报表 %d        十进制有符号整数 %u        十进制无符号整数 %f        浮点数 %s        字符串 %c ...

  2. 第十五节,基本数据类型,元组tuple

    元组和列表的区别 元组和列表几乎是一样的 不一样的地方就是元组创建后元组的元素不可以修改,比如(添加,拓展,移除等修改功能,但是元组里的元素的元素是可以修改的) 基本操作: 索引 切片 循环 长度 包 ...

  3. div+css位置绝对定位和相对定位

    position:absolute: 当div中被隔着些元素的话那么用此方法将把元素重叠在一起,所以元素可以不在容器中加也能重叠在一起

  4. java中的equals()方法

    大家都知道,在Java中,对于对象的比较,如果用“==”比较的是对象的引用,而equals才是比较的对象的内容. 一般我们在设计一个类时,需要重写父类的equals方法,在重写这个方法时,需要按照以下 ...

  5. [转载] 关于Windows Boot Manager、Bootmgfw.efi、Bootx64.efi、bcdboot.exe 的详解

    原帖: http://bbs.wuyou.net/forum.php?mod=viewthread&tid=303679 前言:1.本教程针对于UEFI启动来叙述的,根据普遍的支持UEFI的机 ...

  6. Windows GTK+ 环境搭建(详解)

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc901017ygy.html Windows GTK+ 环境搭建 最近要做界面的一些东西,但是对微软提供的类库MFC不 ...

  7. mongodb备份

    一:数据导出,数据导入作为DBA(管理员),经常会碰到导入导出数据的需求,下面介绍实用工具(自带的):1:数据导入 mongoexport导出csv格式的文件./mongoexport -d Test ...

  8. ZendStudio格式化HTML代码方法及格式化后错位问题修正

    ZendStudio中格式化HTML快捷键:Ctrl+Shift+F 为什么html文件里面的内容格式化完都乱套了? 选 择window菜单->Preferences->Web->H ...

  9. linux(视频学习)2

    第二部分(javaee的开发环境的搭建): 1. 安装jdk的过程: 安装ios的镜像文件,挂载到/mnt目录下.挂载: mount  /mnt/cdrom卸载: umount  /mnt/cdrom ...

  10. python 基本的序列和映射规则

    >>> def checkIndex(key):...     if not isinstance(key,(int,long)):raise TypeError...     if ...