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)

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

时间: 2024-10-10 05:08:41

ASC(2)A(大数+图论DP)的相关文章

ASC(1)C(树形DP)

New Year Bonus Grant Special JudgeTime Limit: 6000/3000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description All programmers of Mocrosoft software company are organized in a strict subordination hi

图论.DP

见题: 看一眼,就知道是个依赖性背包,于是乎就草草的打了树上DP,一交发现才20,仔细检查也没错呀,忍不住点了题解,只喵一眼看到了强联通缩点等的字样,又重新审了一遍题,发现这句话理解有偏差:软件i只有在安装了软件j(包括软件j的直接或间接依赖).题目并未说i依赖j时,j就不能依赖i了,所以就形成了环了. 代码: #include<bits/stdc++.h> #define max(a,b) (((a)>(b))?(a):(b)) #define min(a,b) (((a)<(b

hdoj 5092 Seam Carving 【树塔DP变形 + 路径输出】 【简单题】

Seam Carving Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 956    Accepted Submission(s): 382 Problem Description Fish likes to take photo with his friends. Several days ago, he found that so

Codeforces 18D Seller Bob &amp;&amp; 18E Flag 2 简单dp

D题很恶心的要用大数. dp[i] 表示到第 i 条信息的最大收益. 显然,dp[1]  = 0. 对于i > 1有,若当前信息为 win x,那么显然有dp[i] = dp[i-1]. 若当前信息为sell x,那么dp[i] = max(dp[i-1] , dp[j] + 2^x),j 需满足j < i && 第j条信息为 win y && y == x. import java.util.Scanner; import java.math.BigInteg

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDOJ

第一篇 1001 这个就不用说了吧1002 简单的大数1003 DP经典问题,最大连续子段和1004 简单题1005 找规律(循环点)1006 感觉有点BT的题,我到现在还没过1007 经典问题,最近点对问题,用分治1008 简单题1009 贪心1010 搜索题,剪枝很关键1011 1012 简单题1013 简单题(有个小陷阱)1014 简单题1015 可以看作搜索题吧1016 经典的搜索1017 简单数学题1018 简单数学题1019 简单数学题1020 简单的字符串处理1021 找规律的数学

(转)

来源:http://blog.csdn.net/vsooda/article/details/7293655 1001       这个就不用说了吧1002       简单的大数1003       DP经典问题,最大连续子段和1004       简单题1005       找规律(循环点)1006       感觉有点BT的题,我到现在还没过1007       经典问题,最近点对问题,用分治1008       简单题1009       贪心1010       搜索题,剪枝很关键10

hd题目分类

分类一(详细): 分类二: 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196.1197.1201.1202.1205.1219.1234.12

oj题目分类

注:网上搜的 第一篇    1001 这个就不用说了吧 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最近点对问题,用分治 1008 简单题 1009 贪心 1010 搜索题,剪枝很关键 1011 1012 简单题 1013 简单题(有个小陷阱) 1014 简单题 1015 可以看作搜索题吧 1016 经典的搜索 1017 简单数学题 1018 简单数学题 1019 简单