这是我们校赛的一道题,给一个字符串,判断这是字符串描绘的是不是一个堆,并不难,只是一个简单的模拟,但是也稍微有点麻烦,最起码我的方法代码量比较大,主要用栈做一个父亲与儿子的位置匹配,匹配的方法应该有很多.然后在读入的时候注意数字的读入方法,我一开始只读入了一个数导致出错,后来才改对的
#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[22000]; int match[22000],tot,len; char str[22000]; bool is_a_int(char a); bool is_a_father(int id,int number) { node[tot].num = number; if(str[id+2] == ':') { 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+3] == '[') { start = id+5,length = 0,now = start,end; while(is_a_int(str[now]) && is_a_int(str[now+1])) { length++; now++; } cnt = 0,number = 0; end = start + length; while(cnt <= length) { number += pow(10,cnt) * (str[end-cnt] - '0'); cnt++; } node[tot].lc = number; } else if(str[id+3] == '(') { if(!is_a_int(str[id+4])) node[tot].lc = INF; else { start = id+4,length = 0,now = start,end; while(is_a_int(str[now]) && is_a_int(str[now+1])) { length++; now++; } cnt = 0,number = 0; end = start + length; while(cnt <= length) { number += pow(10,cnt) * (str[end-cnt] - '0'); cnt++; } node[tot].lc = number; } } int douhao_id = match[id+2]; if(str[douhao_id+1] == '(' && str[douhao_id+2] == ')') node[tot].rc = INF; else if(is_a_int(str[douhao_id+2])) { start = douhao_id+2,length = 0,now = start,end; while(is_a_int(str[now]) && is_a_int(str[now+1]) ) { length++; now++; } cnt = 0,number = 0; end = start + length; while(cnt <= length) { number += pow(10,cnt) * (str[end-cnt] - '0'); cnt++; } node[tot].rc = number; } else if(str[douhao_id+1] == '[') { start = douhao_id+3,length = 0,now = start,end; while(is_a_int(str[now]) && is_a_int(str[now+1]) ) { length++; now++; } cnt = 0,number = 0; end = start + length; while(cnt <= length) { number += pow(10,cnt) * (str[end-cnt] - '0'); cnt++; } node[tot].rc = number; } } bool is_a_int(char a) { if(a >= '0' && a <= '9') 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,0,sizeof(match)); for(int i = 0; i < len; i++) { if(str[i] == ':') s.push(i); else if(str[i] == ',') { match[s.top()] = i; s.pop(); } } tot = 0; for(int i = 0; i < len-1; i++) { if(is_a_int(str[i])) { int start_id = i,length = 0; int now_id = start_id; while(is_a_int(str[now_id]) && is_a_int(str[now_id + 1]) ) { length++; now_id++; } int end_id = start_id + length; int cnt = 0,number = 0; while(cnt <= length) { number += pow(10,cnt) * (str[end_id - cnt] - '0'); 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 = 0; 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 = 0; 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 0; }