DFA+dp+zoj2337
来源:互联网 发布:java和php全面对比 编辑:程序博客网 时间:2024/05/22 00:36
In the theory of compilers and languages finite state machines, also known asfinite automata arewidely used. Deterministic finite automation (DFA) is an ordered set<Z, U, s, T, F> where Z is the finiteset called input alphabet,U is the finite set of states, s (belongs to U) is theinitial state, T (is a subset of U) is the set of terminalstates and F : U * Z -> U is the transition function.
The input of the automation is the string z over Z. Initially the automation is in states. Each stepit reads the first character c of the input string and changes its state toF(u, c) where u is the currentstate. After that the first character of the input string is removed and the step repeats. If when its inputstring is empty the automation is in the terminal state, it is said that it accepts the initial stringz, inthe other case it rejects it.
In some cases to simplify the automation the concept of nonabsorbing edges is introduced. That is,in addition toF the function G : U * Z -> {0, 1} is introduced and when making a transition from somestateu with some character c, the leading character is removed from the input string only ifG(u, c) = 0.If G(u, c) = 1, the input string is kept intact and next transition is performed with the new state and thesame character.
It is said that such automation accepts some string z if after a number of steps it transits to theterminal state and the input string becomes empty.
Your task is given the DFA with nonabsorbing edges to compute the number of strings of the givenlengthN that it accepts.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line of the input file contains Z - a subset of the English alphabet, several different smallletters. Next line containsK = |U| - the number of states of the automation (1 <= K <= 1000). Letstates be numbered from 1 toK. Next line contains S (1 <= S <= K) - the initial state, followed byL = |T| - the number of terminal states and thenL different integer numbers ranging from 1 to K -the numbers of terminal states.
Next K lines contain |Z| integer numbers each and define F. NextK lines define G in a similar way.The last line of the input file containsN(1 <= N <= 60).
Output
Output the only number - the number of different strings of length N overZ that the given DFAaccepts.
Sample Input
1
ab
2
1 1 2
2 1
1 2
0 1
0 0
3
Sample Output
2题意感觉很难懂啊,看明白之后就很简单了,就跟AC自动机的dp统计一样了
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=1010;char s[30];int K,S,L,len,N;int l[maxn],f[maxn][28],g[maxn][28];bool to[maxn][28];struct BigInt{ const static int mod = 10000; const static int DLEN = 4; int a[30],len; BigInt() { memset(a,0,sizeof(a)); len = 1; } BigInt(int v) { memset(a,0,sizeof(a)); len = 0; do { a[len++] = v%mod; v /= mod; }while(v); } BigInt(const char s[]) { memset(a,0,sizeof(a)); int L = strlen(s); len = L/DLEN; if(L%DLEN)len++; int index = 0; for(int i = L-1;i >= 0;i -= DLEN) { int t = 0; int k = i - DLEN + 1; if(k < 0)k = 0; for(int j = k;j <= i;j++) t = t*10 + s[j] - '0'; a[index++] = t; } } BigInt operator +(const BigInt &b)const { BigInt res; res.len = max(len,b.len); for(int i = 0;i <= res.len;i++) res.a[i] = 0; for(int i = 0;i < res.len;i++) { res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0); res.a[i+1] += res.a[i]/mod; res.a[i] %= mod; } if(res.a[res.len] > 0)res.len++; return res; } BigInt operator-(const BigInt &b)const { BigInt res,tmp=(*this); res.len=tmp.len; for(int i=0;i<b.len;i++) { if(tmp.a[i]<b.a[i]) { int k=i+1; while(tmp.a[k]==0) { tmp.a[k]=mod-1; k++; } tmp.a[k]--; tmp.a[i]+=mod; } res.a[i]=tmp.a[i]-b.a[i]; } for(int i=b.len;i<tmp.len;i++)res.a[i]=tmp.a[i]; while(res.a[res.len-1]==0&&res.len>1)res.len--; return res; } BigInt operator *(const BigInt &b)const { BigInt res; for(int i = 0; i < len;i++) { int up = 0; for(int j = 0;j < b.len;j++) { int temp = a[i]*b.a[j] + res.a[i+j] + up; res.a[i+j] = temp%mod; up = temp/mod; } if(up != 0) res.a[i + b.len] = up; } res.len = len + b.len; while(res.a[res.len - 1] == 0 &&res.len > 1)res.len--; return res; } void output() { printf("%d",a[len-1]); for(int i = len-2;i >=0 ;i--) printf("%04d",a[i]); printf("\n"); }};BigInt ans[65][maxn];int dfs(int i,int j){ if(!g[i][j]||to[i][j])return f[i][j]; int tmp=f[i][j]; to[i][j]=1; f[i][j]=0; f[i][j]=dfs(tmp,j); return f[i][j];}void solve(){ memset(to,0,sizeof(to)); for(int i=1;i<=K;i++) for(int j=1;j<=len;j++) if(g[i][j])dfs(i,j); for(int i=0;i<=N;i++) for(int j=0;j<=K;j++)ans[i][j]=0; ans[0][S]=1; for(int i=1;i<=N;i++) for(int j=1;j<=K;j++) for(int k=1;k<=len;k++) ans[i][f[j][k]]=ans[i][f[j][k]]+ans[i-1][j]; BigInt sum(0); for(int i=1;i<=L;i++)sum=sum+ans[N][l[i]]; sum.output();}int main(){ int T; scanf("%d",&T); bool first=true; while(T--) { if(first)first=false; else printf("\n"); scanf("%s",s); scanf("%d%d%d",&K,&S,&L); for(int i=1;i<=L;i++)scanf("%d",&l[i]); len=strlen(s); for(int i=1;i<=K;i++) for(int j=1;j<=len;j++)scanf("%d",&f[i][j]); for(int i=1;i<=K;i++) for(int j=1;j<=len;j++)scanf("%d",&g[i][j]); scanf("%d",&N); solve(); } return 0;}
- DFA+dp+zoj2337
- Non Absorbing DFA DP (ASC2A SGU201 ZOJ2337 ACdream1218 Gym100197A)
- acdream 1218 DFA+DP
- dfa
- dfa
- dfa
- dfa
- DFA
- DFA
- dfa
- SGU 201 Non Absorbing DFA (DP)
- hdu 2604 queuing dfa dp + 矩阵快速幂
- AOJ1127 Non Absorbing DFA(自动机+DP)from ASC2
- dda dfa
- DFA&NFA
- nfa&dfa
- NFA/DFA
- #21. DFA
- 架构师修炼 后记 - 推荐必读的巨著
- [IOS]新浪微博项目1.0
- jQ中遇到的小问题-----Jquery调用出现ReferenceError: $ is not defined
- 网络协议 知识汇总(整理中)
- 数据结构实验一(类模板与多文件结构) 终于补上...
- DFA+dp+zoj2337
- C++经典面试题--单例模式
- U-boot源码解析
- Mahout 第一章 学习笔记 思维导图
- 搜索查找 - 算法学习
- fedora 20 可用的键盘快捷键(官方帮助文档)
- mbed OS - ARM关于物联网(IoT)的战略布局
- Populating Next Right Pointers in Each Node
- PAT 3-05 求链式线性表的倒数第K项(C语言实现)