ZOJ1011 NAT

NTA

Time Limit: 2 Seconds      Memory Limit: 65536 KB

The NTA (Non-deterministic Tree Automata) is a kind of tree structure device. The device is built in a set of operating rules. With these rules the device can produce several signals, which will form a signal system. In such a system, one signal is the starting signal, several signals are the acceptable signals, and the others are the auxiliary ones. A pair of signals is said to be an acceptable pair if both two signals of the pair are acceptable.

The trees discussed here are all binary trees. Every non-leaf node has two successors. In any finite tree, each node has a signal-transmitting element. When a signal arrives at one node, the signal meets the signal transmitting substance, and triggers off signal reactions, which will produce several pairs of signals. Then the device selects a pair of signals non-deterministically and sends them to its successors. The first signal in the signal pair is sent to the left successive node and the second one is sent to the right successive node.

The whole operation for an NTA is as follows:

The device first sends the starting signal to the root node. According to the signal transmitting substance at the root node, the device selects a pair of signals non-deterministically and sends the first to the left son and the second to the right son. Each of the two signals then meets the signal transmitting substance at the corresponding node and produces another two signals. The course proceeds down until the signals arrive at the leaves.

If a signal reaches one leaf and the leaf can produce a pair of acceptable signals, we say the leaf is "shakable". A transmission of signals from the root to leaves is said to be valid if all leaves are "shakable". A tree structure with signal transmitting substance is valid if there exists such a valid transmission. A tree is invalid if all the transmissions are invalid.

For simplicity, we denote the signal transmitting elements by consecutive lowercase letters "a", "b", "c", etc.. The signals of an NTA are consecutive numbers 0,1,2, ..., and so on. The first signal 0 is always a starting signal. Thus the signals for a 4-signal NTA are "0" "1" "2" and "3". Accepting signals are arranged at the end of the number sequence so that if a 4-signal NTA has two accepting signals, the accepting signals are "2" and "3". The transition rules of signals are based on a transition table. For example, the following table describes a transition table with four signals "0", "1", "2", "3" and with three signal transmitting elements "a", "b" and "c".

 

Tabc
0(1,2)(2,1)(1,0)
1(2,2)(0,2),(1,0)(3,2)
2(2,2)(2,3)(1,2)
3(1,2)(2,1)(3,2)

 

In this transition table some reactions of signals on certain signal transmitting elements are deterministic, and others are non-deterministic. In the example above, if signal "1" reaches the node with the transmitting element "b", the reaction is non-deterministic.

Now your task is to write a program to judge if a tree structure with certain signal transmitting substance is valid.


Input

The input file contains several cases. Each case describes a sequence of NTA descriptions and some initial tree configurations. The first line for each case consists of three integers n, m and k. The integer n is the number of signals, m indicates the number of accepting signals, and k is number of signal transmitting elements. The following n k lines describe the transition table in row-major order. Each transition of a signal on signal transmitting element is given on a separate line. On such line every two numbers represent a possible transition.

This is followed by the description of tree structures. For every tree structure a number L is given on a separate line to indicate the level of the tree. The following L+1 lines containing a sequence of letters describe the tree structure. Each level is described in one line. There exist one space between two successive letters. The 0-th level begins firstly. In the tree structure, the empty nodes are marked by "*". The tree structure with L=-1 terminates the configurations of tree structures for that NTA, and this structure should not be judged.

The input is terminated by a description starting with n=0, m=0 and k=0. This description should not be processed.

Note: In each case, NTA will have at most l5 signals and 10 characters. The level of each tree will be no more than 10.


Output


For each NTA description, print the number of the NTA (NTAl, NTA2, etc.) followed by a colon. Then for each initial tree configuration of the NTA print the word "Valid" or "Invalid".

Print a blank line between NTA cases.


Sample Input

4 2 3
1 2
2 1
1 0
2 2
0 2 1 0
3 2
2 2
2 3
1 2
1 2
2 1
3 2
3
a
b c
a b c b
b a b a c a * *
2
b
a b
b c * *
-1
0 0 0


Output for the Sample Input

NTA1:
Valid
Invalid


Source: Asia 2001, Shanghai (Mainland China)

题意:把握题目意思有点难,英语水平和耐心要有

主要算法:回溯算法

难点:数据的输入存入结构

感悟:要灵活运用STL;关于递归算法的设计,可以先想好结束条件,再处理其他。

代码:

#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;

//n代表信号个数,m是合法信号,k为信号发射单元的个数
int n,m,k;                             
int treeLevel;                                      //二叉树的深度,从根为0开始
int treeDeep;                                       //二叉树结点的编号,从根为0开始
char tree[1000];
struct signal                                       //存放结点的结构
{
    int left,right;
};

vector<signal> table[20][20];

signal pair;

void readTable()
{
    char ch;
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<k;j++)
        {
            table[i][j].clear();
            while(true)
            {
                signal pair;
                scanf("%d%d",&pair.left,&pair.right);
                table[i][j].push_back(pair);
                ch = getchar();
                if(ch == '\n') break;
            }
        }
    }
}

void readTree()
{
    char ch;
    treeDeep = 0;
    int i,j;
    for(i = 0;i<=treeLevel;i++)
    {
        for(j = 0;j<(1<<i);j++)
        {
            cin>>ch;
            tree[treeDeep] = ch;
            treeDeep++;
        }
    }
}

bool judge(int signal, int node)
{
    int signal1,signal2;
    if(tree[node] =='*' || node>=treeDeep)
    {
        if(signal<n-m)return false;
        else return true;
    }
    int k1 = tree[node]-'a';
    int left = node*2+1;
    int right = left+1;

    for(int i = 0;i<table[signal][k1].size();i++)
    {
        signal1=table[signal][k1][i].left;
        signal2=table[signal][k1][i].right;
        if(judge(signal1,left)&&judge(signal2,right)) return true;
    }
    return false;
}

int main()
{
    freopen("input.txt","r",stdin);
    int casent = 1;
    while(cin>>n>>m>>k&&(n!=0 ||m!=0 ||k!=0))
    {
        cout<<"NTA"<<casent<<":"<<endl;
        casent++;
        readTable();
        while(cin>>treeLevel&&treeLevel!=-1)

        {
            readTree();
        }


        if(judge(0,0))
        {
            cout<<"Valid"<<endl;
        }
        else
        {
            cout<<"Invalid"<<endl;
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值