201. Non Absorbing DFA
题意好难看懂的说。。。
有限状态自动机DFA是这么一个有序组<Σ, U, s, T, phi>;Σ代表输入字符集,表示此自动机的工作范围;U代表所有的状态集合;s是初始状态;T是最终状态;phi代表转移函数,定义为phi : U × Σ → U。
利用DFA进行字符串识别是要你做这么一件事情: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.
然后无聊的出题人给他加入了一个,b的东西:Nonabsorbing Edges,直译不吸收边。Define function χ : U × Σ → {0, 1}. 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.
称这样的DFA为NADFA。
NADFA接受字符串的条件是:such automation accepts some string α if after a number of steps it transits to the terminal state and the input string becomes empty.
输入:第一行是字符集Σ。下来一行是k,表示状态数量。下一行第一个数字S代表初始状态,第二个数字L代表结束状态的数量,然后是L个数字,代表结束状态的编号。然后是一个k * |Σ|的矩阵,代表函数phi。然后再是一个同样大小的矩阵,代表函数X。最后是N。
题目就是要你求能被此,b形式下的DFA(NADFA)接受的长度为N的字符串数量。
竟然是ASC#2的A题!!!
然后发现SGU200~2**都是ASC的原题。。不忍直视了。。以我的智商看来SGU都刷不了了。。
不过SGU200题说到做到,决不放弃!BZOJ用来专门练一些算法。
怎么搞??
DP是很显然的。用f[i][j]表示匹配到第i位时处在状态j的字符串数量。由于不可吸收边的存在,可以使用记忆化搜索预处理出状态的直接转移。
要用到高精度。。。压位大法好。。。
//{HEADS
#define FILE_IN_OUT
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
#include <string>
#define REP(i, j) for (int i = 1; i <= j; ++i)
#define REPI(i, j, k) for (int i = j; i <= k; ++i)
#define REPD(i, j) for (int i = j; 0 < i; --i)
#define STLR(i, con) for (int i = 0, sz = con.size(); i < sz; ++i)
#define STLRD(i, con) for (int i = con.size() - 1; 0 <= i; --i)
#define CLR(s) memset(s, 0, sizeof s)
#define SET(s, v) memset(s, v, sizeof s)
#define mp make_pair
#define pb push_back
#define PL(k, n) for (int i = 1; i <= n; ++i) { cout << k[i] << ' '; } cout << endl
#define PS(k) STLR(i, k) { cout << k[i] << ' '; } cout << endl
using namespace std;
void FILE_INIT(string FILE_NAME) {
#ifdef FILE_IN_OUT
#ifndef ONLINE_JUDGE
freopen((FILE_NAME + ".in").c_str(), "r", stdin);
freopen((FILE_NAME + ".out").c_str(), "w", stdout);
#endif
#endif
}
typedef long long LL;
typedef double DB;
typedef pair<int, int> i_pair;
const int INF = 0x3f3f3f3f;
//} const int maxn = + ;
const int maxl = + ;
const int maxm = + ;
const int maxc = ;
const int base = ; char sigma[maxc];
int Snum, Size, S, k, L, n;
int teminal_state[maxn], phi[maxn][maxc], chi[maxn][maxc], trans[maxn][maxc]; struct big_int {
int d[maxl];
int len;
big_int() {
CLR(d);
len = ;
}
big_int &operator += (const big_int &a) {
len = max(len, a.len);
REP(i, len) {
d[i] += a.d[i];
if(base < d[i]) {
d[i + ] += d[i] / base;
d[i] %= base;
}
}
for(; < d[ + len]; ++len);
return *this;
}
void print() {
printf("%d", d[len]);
for(int i = len - ; < i; --i) {
printf("%09d", d[i]);
}
puts("");
}
}f[maxn][maxm]; void dfs(int u, int c) {
if(chi[u][c] == ) {
trans[u][c] = u;
}
if(trans[u][c]) {
return;
}
trans[u][c] = -;
dfs(phi[u][c], c);
trans[u][c] = trans[phi[u][c]][c];
} int main() {
FILE_INIT(""); scanf("%s", sigma);
Size = strlen(sigma);
scanf("%d", &k);
scanf("%d %d", &S, &L);
REP(i, L) {
scanf("%d", &teminal_state[i]);
}
REP(i, k) {
REP(j, Size) {
scanf("%d", &phi[i][j]);
}
}
REP(i, k) {
REP(j, Size) {
scanf("%d", &chi[i][j]);
}
}
scanf("%d", &n);
REP(i, k) {
REP(j, Size) {
if(!trans[i][j]) {
dfs(i, j);
}
}
}
f[S][].d[] = ;
REPI(i, , n - ) {
REP(j, k) {
REP(c, Size) {
if(trans[j][c] > ) {
f[phi[trans[j][c]][c]][i + ] += f[j][i];
}
}
}
}
big_int ans;
REP(i, L) {
ans += f[teminal_state[i]][n];
}
ans.print(); return ;
}
201. Non Absorbing DFA的更多相关文章
- SGU 201 Non Absorbing DFA (DP)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个自动机,给出所有的转移,其中还有一个 ...
- AC自动机-算法详解
What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...
- NFA与DFA
正则表达式匹配,包含两个东西,一个是表达式,一个文本. NFA(Nondeterministic Finite Automaton),不确定有穷自动机,表达式主导,NFA去吃文本,贪婪算法吃下去,如果 ...
- 基于DFA敏感词查询的算法简析
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中需要对敏感词做一个过滤,首先有几个方案可以选择: a.直 ...
- java实现敏感词过滤(DFA算法)
小Alan在最近的开发中遇到了敏感词过滤,便去网上查阅了很多敏感词过滤的资料,在这里也和大家分享一下自己的理解. 敏感词过滤应该是不用给大家过多的解释吧?讲白了就是你在项目中输入某些字(比如输入xxo ...
- 使用DFA做文本编辑器的自动提示
之前看龙书的时候,龙书提到可以在编译器里用动态的生成的NFA自动机来动态匹配自己的输入串,NFA的简单实现其实写起来非常简单,但是我是实际凭感觉写完之后,却觉得并不是非常的好用,在处理自己已经输入过的 ...
- DFA 最小化
NDFA.εNDFA 确定化的细节这里就不总结了,这里说一说DFA最小化的算法. 关于DFA最小化,
- C# 词法分析器(五)转换 DFA
系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 在上一篇文章中,已经得到了与正则表达式等价的 NFA ...
- NFA转DFA - json数字识别
json的主页上,提供了number类型的符号识别过程,如下: 图片引用:http://www.json.org/json-zh.html 实际上这张图片表示的是一个状态机,只是状态没有标出来.因为这 ...
随机推荐
- stout代码分析之十一:hashmap和multihashmap
hashmap是std::unordered_map的子类,前者对后者的接口做了进一步封装. hashmap的移动构造函数: hashmap(std::map<Key, Value>&am ...
- CentOS 下安装 LEMP 服务(nginx、MariaDB/MySQL 和 php)
转载自:https://linux.cn/article-4314-1.html 编译自:http://xmodulo.com/install-lemp-stack-centos.html 作者: D ...
- UVA 11105 Semi-prime H-numbers
https://vjudge.net/problem/UVA-11105 筛法 #include<cstdio> #include<cstring> #define N 100 ...
- 2015/8/29 Python基础(3):数值
数字提供了标量储存和直接访问,是不可更改类型,每次变更数值会产生新的对象.Python支持多种数字类型,包括整型.长整型.布尔型.双精度浮点.十进制浮点和复数.在Python中,变量并不是一个盒子,而 ...
- CharSequence 去除两端空格
CharSequence就是字符序列,String, StringBuilder和StringBuffer都是其实现类. 模仿String.trim() 实现了一个CharSequence通用的去除两 ...
- 转【jenkins插件】
开源版本的Jenkins 具有三大能力:Master-Slave的分布式构建调度能力.Pipeline编排能力.强大的开源生态(插件)能力. 2017年4月,Jenkins创始人KK(Kohsuke ...
- shell 25个常用命令
1.列出所有目录使用量,并按大小排序. ls|xargs du -h|sort -rn #不递归下级目录使用du -sh 2.查看文件排除以#开关和空白行,适合查看配置文件. egrep -v ...
- [uva11137]立方数之和·简单dp
小水题再来一发 给定一个正整数n<=1e4,求将n写成若干个正整数立方和的方法数 典型的多阶段模型 f[i][j]表示当前用到1~i的数,累计和为j的方案数. #include<cstdi ...
- 微软Azure DevOps 使用docker 持续集成 dotnet
azure 环境设置 登录azure 地址 https://dev.azure.com/ 使用微软账号就可以进行登录. 点击右上角新建项目 项目信息,尽量用小写 创建项目 修改默认的dockerfil ...
- 【Python学习笔记】Coursera课程《Using Databases with Python》 密歇根大学 Charles Severance——Week4 Many-to-Many Relationships in SQL课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Week4 Many-to-Many Relationships in SQL 15.8 Man ...