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

#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 ;
}

最新文章

  1. c# WebClient文件下载
  2. Jbuilder 2008安装及破解
  3. js数组常用方法汇总
  4. shell脚本批量处理字符串
  5. 电赛总结(四)&mdash;&mdash;波形发生芯片总结之AD9851
  6. 连接ssh反应很慢,卡,延迟
  7. Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
  8. oracle的sql函数
  9. Windows中安装Emacs
  10. Caffe —— Deep learning in Practice
  11. iOS ARC与MRC混编的一些解决方法
  12. 最简单的基于DirectShow的示例:视频播放器
  13. [CQOI2018]破解D-H协议
  14. 微信小程序入门教程(一)API接口数据记录
  15. Python-字符串的常用操作
  16. cartographer 安装问题
  17. win常用
  18. 去除idea15重复代码校验
  19. JAVA中字符串问题
  20. jsp中的一些细节和注意要点。。。。。简记

热门文章

  1. 用VS2012或VS2013在win7下编写的程序在XP下运行就出现“不是有效的win32应用程序
  2. Java中的String[] args
  3. Java C# .net 和 C C++ 跨平台的区别
  4. php - preg_match
  5. 如何清除jboss缓存
  6. photoshop 魔术橡皮擦
  7. PHP的curl实现get,post 和 cookie
  8. Java中的设计模式
  9. springmvc中的几个问题
  10. 一款值得推荐的shell工具