题目大意:有一颗完全二叉树,给节点一个信号会从一个表中选择一对信号分别传递给两个子节点。最后判断所有叶子节点是否满足给定的规则。题目有点长,具体可参见原题。

  首先是表格中数据的存储,由于会有多个元素,用vector进行保存。其次,树是完全二叉树,可以用数组存储树。然后就是遍历二叉树了。

 #include <iostream>
#include <cstdio>
#include <vector>
using namespace std; struct Signal
{
int left, right;
};
vector<Signal> table[][];
char tree[]; int n, m, k;
// n is the number of signals, m is the number of accepting signals, k is the number of signal transmitting elements.
int l; // the level of the tree
int nodeN; // the number of nodes of the tree void readTable()
{
for (int i = ; i < n; i++)
for (int j = ; j < k; j++)
{
table[i][j].clear();
Signal pair;
while (true)
{
scanf("%d%d", &pair.left, &pair.right);
table[i][j].push_back(pair);
char ch = getchar();
if (ch == '\n') break;
}
}
} void readTree()
{
char ch;
nodeN = ;
for (int i = ; i <= l; i++)
for (int j = ; j < (<<i); j++)
{
cin >> ch;
tree[++nodeN] = ch;
}
} bool judge(int signal, int node)
{
if (tree[node] == '*' || node > nodeN)
{
if (signal < n-m) return false;
else return true;
}
int t = tree[node] - 'a';
for (int i = ; i < table[signal][t].size(); i++)
{
int signal1 = table[signal][t][i].left;
int signal2 = table[signal][t][i].right;
if (judge(signal1, node*) && judge(signal2, node*+)) return true;
}
return false;
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int kase = ;
while (scanf("%d%d%d", &n, &m, &k) && (n || m || k))
{
if (kase++) printf("\n");
printf("NTA%d:\n", kase);
readTable();
while (scanf("%d", &l) && (l != -))
{
readTree();
if (judge(, )) printf("Valid\n");
else printf("Invalid\n");
}
}
return ;
}

  vector就用过几次,算是熟悉了一下vector的用法吧,clear()清除内容,另外常用的就是puch_back()了,其他的还有pop_back()和insert()。还有那个利用'\n'判断是否还有数据的方法比用读整行字符串再处理简单多了,然后就是知道了用 cin >> ch 读字符可以跳过空格。最后用递归判断所有叶子节点是否符合要求的思想也让我长见识啦:D。因为输出时把NTA写成NAT,WA了一次,无语...

ZOJ 1011 - NTA的更多相关文章

  1. [ZOJ 1011] NTA (dfs搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...

  2. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  3. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  4. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  5. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. SCNU 2015ACM新生赛初赛【1001~1011】个人解题思路

            题目1001:       大意:已知$n$个角色,$m$种怪物种族,$k$个怪物,给出一组角色编号,编号$P_{i}$的角色能肝死编号$i$的怪物,对于给定的一组怪物编号,为了打通关 ...

  8. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  9. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

随机推荐

  1. C#入门经典第八章面向对象编程简介-1

    面向对象编程(Object-Oriented Programming,OOP)技术 本章中的OPP实际上是.NET OOP,这里讲的一些技术不能应用于其他OOP环境.

  2. C#入门经典第四章-流程控制-1

    布尔类型:

  3. wireshark 使用技巧

      Wireshark使用技巧-GeoIP显示IP地理位置     在使用Wireshark时,有的时候需要知道抓取的报文中某个IP地址的具体地理位置,笨一点的方法是将IP地址复制,然后通过一些软件或 ...

  4. python正则表达式例子说明

    pattern = re.compile('<div.*?author">.*?<a.*?<img.*?>(.*?)</a>.*?<div.* ...

  5. PHP文件夹操作

    文件:文件+目录 判断文件类型: filetype("路径"); //返回一个字符串 is_dir("路径"); //如果是目录会返回true 判断文件是不是目 ...

  6. HDU 5171 GTY's birthday gift 矩阵快速幂

    GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. Android源码之Gallery专题研究(1)

    前言 时光飞逝,从事Android系统开发已经两年了,总想写点什么来安慰自己.思考了很久总是无法下笔,觉得没什么好写的.现在终于决定写一些符合大多数人需求的东西,想必使用过Android手机的人们一定 ...

  8. HDU 2498 Digits

    水题.题目这样定义的,另f(x)为x有几位,x[i]=f(x[i-1]); 求最小的i使得x[i]==x[i-1] #include<cstdio> #include<cstring ...

  9. 简单的字符串比较题 POJ 1936

    Description You have devised a new encryption technique which encodes a message by inserting between ...

  10. OPENCV条形码检测与识别

    条形码是当前超市和部分工厂使用比较普遍的物品,产品标识技术,使用摄像头检测一张图片的条形码包含有两个步骤,第一是定位条形码的位置,定位之后剪切出条形码,并且识别出条形码对应的字符串,然后就可以调用网络 ...