ASC (2) A (large number + Graph Theory DP)

Source: Internet
Author: User
Tags integer numbers
Non absorbing dfatime limit: 10000/5000 ms (Java/others) memory limit: 128000/64000 KB (Java/others) submitstatisticnext problemproblem description

In the theory of compilers and ages finite state machines, also known as finite automata are widely used. deterministic Finite Automation (DFA) is an ordered set <Σ, U, S, T, Phi> 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 Phi: 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 Phi (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 Phi the function Syntax: 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 Random (u, c) = 0. if substring (u, c) = 1, the input string is kept intact and next transition is Med 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 Phi. next K lines define finished 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
ab21 1 22 11 20 10 03
Sample output
2
Hintin the given example the two strings accepted by the automation are "AAA" and "ABB ".
Question: It's disgusting. I feel like I have to say a lot or don't say =. At that time, I was crying.
Idea: we can regard the automatic machine as a directed graph, the state as a vertex, the letter as an edge, and then transfer it according to the value given by the # matrix.
First, we need to pre-process the graph according to the X [u] [c] function. If X [u] [c] = 1, we will expand along this edge, if a ring is formed, this routing method cannot be used.
Otherwise, mark the next step of this State as the earliest possible state (X [U1] [C1] = 0)
Let's take a look at the code. We have written recursive and recursive versions. recursion is faster.

ASC (2) A (large number + Graph Theory DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.