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

  首先是表格中数据的存储,由于会有多个元素,用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. I2C死锁原因及解决方法(转)

    源:http://blog.csdn.net/zyboy2000/article/details/5603091 死锁总线表现为:SCL为高,SDA一直为低 现象:单片机采用硬件i2c读取E2PROM ...

  2. OGG FAQ

    Q1:oracle_关于参数.ENABLE_GOLDENGATE_REPLICATION A:   So, in order to use OGG, on Oracle 11.2.0.4, or Or ...

  3. GridView 编辑,更新,删除 等操作~~

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1. ...

  4. CALayer & UIView 关系浅析

    原文链接:http://www.jianshu.com/p/8e6a313c158e 一.CALayer和UIView的关系 UIView显示在屏幕上归功于CALayer 可以说:UIView依赖CA ...

  5. ViewController 视图控制器

    [父视图控制器 addChildViewController:子视图控制器]; 在此,图控制器A添加了另一个图控制器B,那么A充当父视图控制器,B充当子视图控制器.父视图控制器充当了视图控制器容器的角 ...

  6. Android性能优化典范---转

    Android性能优化典范 原文链接:  http://hukai.me/android-performance-patterns/ JAN 17TH, 2015 | COMMENTS 2015新年伊 ...

  7. dedecms自定义表单

  8. 树列复选框Extjs

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. Python之路:堡垒机实例以及数据库操作

    一.堡垒机前戏 开发堡垒机之前,先学习Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作. SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: ...

  10. Myeclipse配置优化

    http://blog.csdn.net/chen_zw/article/details/7525277