首页 > 代码库 > ASC(2)A(大数+图论DP)

ASC(2)A(大数+图论DP)

Non Absorbing DFA

Time Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
SubmitStatisticNext Problem

Problem Description

      In the theory of compilers and languages finite state machines, also known as finite automata are widely used. Deterministic finite automation (DFA) is an ordered set <Σ, U, s, T, φ>where Σ is the finite set called input alphabet, U is the finite set of states, s ∈U is the initial state, T ? U is the set of terminal states and φ : U × Σ → U is the transition function.

      The input of the automation is the string α over Σ. Initially the automation is in state s. Each step it reads the first character c of the input string and changes its state to φ(u, c) where u is the current state. After that the first character of the input string is removed and the step repeats. If when its input string is empty the automation is in the terminal state, it is said that it accepts the initial string α, in the other case it rejects it.

      In some cases to simplify the automation the concept of nonabsorbing edges is introduced. That is, in addition to φ the function Х : U × Σ → {0, 1} is introduced and when making a transition from some state u with some character c, the leading character is removed from the input string only if Х(u, c) = 0. If Х(u, c) = 1, the input string is kept intact and next transition is performed with the new state and the same character.

      It is said that such automation accepts some string α if after a number of steps it transits to the terminal state and the input string becomes empty.

      Your task is given the DFA with nonabsorbing edges to compute the number of strings of the given length N that it accepts.

Input

      The first line of the input file contains Σ - a subset of the English alphabet, several different small letters. Next line contains K = |U| - the number of states of the automation (1 ≤ K ≤ 1000). Let states be numbered from 1 to K. Next line contains S (1 ≤ S ≤ K) - the initial state, followed by L = |T| - the number of terminal states and then L different integer numbers ranging from 1 to K - the numbers of terminal states.

      Next K lines contain |Σ| integer numbers each and define φ. Next K lines define Х in a similar way. The last line of the input file contains N(1 ≤ N ≤ 60).

Output

      Output the only number - the number of different strings of length N over Σ that the given DFA accepts.

Sample Input

ab
2
1 1 2
2 1
1 2
0 1
0 0
3

Sample Output

2

Hint

In the given example the two strings accepted by the automation are “aaa” and “abb”.

题意:比较恶心,感觉要说一大堆还是不说了= =,当时这题也是看哭了

思路:可以将自动机看成一个有向图,将状态看成点,将字母看成边,然后按照∮矩阵给出的值转移即可

            首先要根据X[u][c]函数将图预处理,如果X[u][c]=1则沿着这条边拓展,如果形成一个环则这种走法一定不可以

            否则将该种状态的下一步直接标记为最早能走的状态(X[u1][c1]=0)

            细节看代码吧,写了递归和递推两种版本,递归还要快点

<script src="https://code.csdn.net/snippets/479139.js" type="text/javascript"></script>

ASC(2)A(大数+图论DP)