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

#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. hdu_2224_The shortest path(dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2224 题意:双调欧几里德旅行商经典问题,找一条最短回路使得该路经过所有的点 题解:dp[i][j]=d ...

  2. Bootstrap 3 与 Foundation 5

    开发工程师, 使用 Bootstrap. 前端开发人员, 使用 Foundation. 我们来谈谈为什么. Bootstrap 与 Foundation 有许多关键的区别, 但是, 我想你只需要记住一 ...

  3. Away 3d 入门demo

    Away3d是不错的开源Flash 3D引擎,现在最新的版本是4.0,在这个例子中我们使用现在比较稳定的3.6版本,4.0API相较之前变化较大,支持最新的flash player11硬件加速 现在写 ...

  4. Java&&As3.0 中的final 关键字

    Java和AS3.0关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效率.   可以修饰的对象:   fin ...

  5. javascript克隆一个对象

    /* * 克隆一个对象 */ com.ty.repairtech.JsonOperation.clone = function(obj) { // Handle the 3 simple types, ...

  6. Android Studio 如何将包名按层级展示

    在project视图右上角有个“设置”的按钮,点开,然后将上图所圈部分去勾选就可以了.

  7. screen 链接远程桌面

    screen     开一个新的screen窗口 screen -ls 查看已经存在的所有screen窗口 screen -r  208111  进入这个窗口 ctrl+a+d  退出screen,回 ...

  8. mongodb分片认证

    启动configsvr 1. 确保mongdb的configsvr是采用service模式启动的,即从/etc/init.d下的脚本启动的,其用户是mongod. 2. 确保mongod的配置文件完全 ...

  9. Struts2 知识体系

    1.struts2是什么 struts2是用于企业级Web开发的框架,基于struts2开发Web应用程序,在开发效率.可扩展性.可维护性上都会大有提升. 2.struts2的优点 struts2框架 ...

  10. FirstOrDefault()的重载方法

    FirstOrDefault方法的使用总结: 现有一集合对象list, 其中集合对象调用FirstOrDefault()方法, list.FirstOrDefault()返回集合中第一个元素, 若集合 ...