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

  首先是表格中数据的存储,由于会有多个元素,用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. ArrayList遍历的同时删除--- 删除还是用迭代器的比较好,其它的都会有问题.

    http://javag.iteye.com/blog/403097 ArrayList遍历的同时删除-- 删除还是用迭代器的比较好,其它的都会有问题.     博客分类: 随笔 Javathread ...

  2. sphinx分域搜索

    http://stackoverflow.com/questions/2526407/complex-query-with-sphinx 比如要实现和如下sql代码相同的功能: SELECT * FR ...

  3. angularjs ng-switch

    <p> <a href="#" ng-click="toggle()">Toggle Section</a> </p& ...

  4. Nginx反向代理配置文件

    server { listen ; server_name ; root E:/Upays/public/; index index.php index.html; log_not_found off ...

  5. PHP 命名空间以及自动加载(自动调用的函数,来include文件)

    这篇文章的目的是记录 1. php中的自动加载函数 __autoload(), 和 spl_autoload_register()函数, 2 .php中命名空间的使用. 一.当不使用命名空间的时候 a ...

  6. angular指令系列---多行文本框自动高度

    angular.module('MyApp') .directive('autoTextare', ['$timeout', function ($timeout) { return { restri ...

  7. sql server 行转列 Pivot UnPivot

    SQL Server中行列转换 Pivot UnPivot 本文转自:张志涛 原文地址: http://www.cnblogs.com/zhangzt/archive/2010/07/29/17878 ...

  8. 跨域访问解决方案:JSONP

    关于什么是跨域请求,可以参见我之前的博文:http://www.cnblogs.com/LiuChunfu/p/5240145.html 上述博文最后有提到解决方案,一直说补充,但是工作忙忘了,直到朋 ...

  9. svn代码管理的使用工作流程

    1. 新建代码库repository. 2. checkout 到workspace. 3. checkin 回 repository. 4. release 一个版本出来(相当于拉出一个branch ...

  10. Netbeans 6.8 + apktool_2.0.0b9 动态调试smali文件

    前言 很早就知道用Netbeans能够单步调试smali,一直拖到现在才真正的自己实现了一次~ 下面是详细步骤! 0×1 环境及工具 a.apktool_2.0.0b9 下载地址:http://con ...