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

#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. leetcode21

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. css字体设置

    css字体设置 .selector{ font-family:"Microsoft YaHei",微软雅黑,"MicrosoftJhengHei",华文细黑,S ...

  3. Linked List 实例

    文件功能:实现了动态建立一个学生信息的链表包括链表的创建.插入.删除.和打印输出学生信息包括姓名和分数 #include<iostream> #include<string> ...

  4. openwrt下加载snmp模块

    加snmp模块到openwrt中去 1.下载snmp的解压包文件 net-snmp-5.4.2.1.tar.gz 下载地址为:http://www.net-snmp.org/download.html ...

  5. Linux 解决 bash ./ 没有那个文件或目录 的方法

    在Debian 或 ubuntu 64位下运行 ./xxx 会跳出来说没有这个文件或者目录,但是ls看又有这个文件,很是奇怪. 其实原因很简单,是因为他没有32位的运行库 ia32-libs ,直接安 ...

  6. hud 2099

    #include <stdio.h> #include <stdlib.h> int main() { int m,n,i,flag; ) { flag=; && ...

  7. sublime 2

    just baidu sublime license and find a free one!

  8. org.hibernate.exception.JDBCConnectionException: could not execute query

    最近在做一个项目,测试的时候是没有问题的,但是放到服务器上以后,第二天就会出现下面的异常.重启Tomcat服务器后就正常了,但是下一天还是会出现同样的异常..... 我就查了一些资料最终把问题给解决了 ...

  9. String 转Map(基于Guava类库)

    字符串格式:key1=value1&key2=value2...... java代码 Map<String,String> targetOrderObj = Splitter.on ...

  10. 从今日起,我会把OpenGL红宝书上的例子用完整的代码形式写在我的博客中,

    1.使用教程:OpenGL红宝书第8版 2.使用的库工具:GLEW和GLFW 3.使用的IDE:vs2012 4.说说目的:完整的看一遍OpenGL,加深印象并且熟练掌握运用OpenGL 5.欢迎有相 ...