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

#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. OpenCV 图片尺寸调整

    http://blog.csdn.net/xiaoshengforever/article/details/12191303 2013-09-30 12:21 10842人阅读 评论(0) 收藏 举报 ...

  2. 天津工业大学CST专业培养方案

    第二学期 网页设计基础 计算机前沿技术 大学生心理健康与人生发展 高级语言程序设计II 思想道德修养与法律基础 职业生涯规划 大学英语二级(A) 大学物理(理三1) 线性代数 高等数学(理一2) 体育 ...

  3. 贾扬清分享_深度学习框架caffe

    Caffe是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的 贾扬清,目前在Google工作.本文是根据机器学习研究会组织的online分享的交流内容,简单的整理了一下. 目录 ...

  4. 在Linux下编写php扩展

    以下内容是本人学习过程中的笔记或者心得,如果有什么建议或者意见请在评论中提醒我,谢谢,这篇文章我会定期更新,由浅到深的分享我学PHP扩展历程 或者在学习中有什么问题欢迎交流 1.去PHP官网下载一个源 ...

  5. 推翻自己和过往,重学自定义View

    http://blog.csdn.net/lfdfhl/article/details/51671038 深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 A ...

  6. myeclipse 之 快捷键

    简单记录一下,新装了个机器,win7系统,想设置一下自己习惯的快捷键 如:ctrl+alt+方向键,复制行,发现设置不上,原先的自带的也失效,设置一下ctrl+alt+其它键,ok可以使用,这说明某些 ...

  7. redis学习一

    一.简介: 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.MongoDB ...

  8. android全屏和取消全屏 旋转屏幕

    全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 取消全屏 getWindow().clearFlags(Win ...

  9. __declspec(dllexport) 和 __declspec(dllimport)的作用

    operatordll.h #include <iostream> #ifndef _WIN32 #define DLL_EXPORT#else #ifdef OPERATORDLL_EX ...

  10. javaweb作業中的幾個要點

    1.DDoS攻击原理DDoS是指分布式拒绝服务(Distributed Denial of Service):试图通过恶意请求使系统或者网络超载进而无法继续提供服务.对于一个网站来说,这意味着,该网站 ...