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 实际上这张图片表示的是一个状态机,只是状态没有标出来.因为这 ...
随机推荐
- 后端日期类属性date 不接受string类型日期,都是没找到解决的方法,所有前端传回的string字符串都一一转化为java定义的类型
1.比如日期 我们可以是yyyy-MM-dd 亦可以是 yyyy-MM-dd HH:mm:ss 方法1在java代码中需要的字段上加上注解 写上日期类型,不过这样很麻烦,每个人写了日期类型的接收前端的 ...
- C. Line (扩展欧几里得)
C. Line time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- python练习:抓取统计log内ip数量
#!/usr/bin/python #-*- coding: utf- -*- import os import re rawfile = '/var/log/auth.log' def rawpar ...
- Android如何在初始化的时候获取加载的布局的宽高
在自定义ListView中,需要将下拉刷新的View在初始化的时候设置padding隐藏起来,这时就要在初始化的时候获得要加载的布局View的高度. private View headView; he ...
- AngularJs学习——常用表单指令练习
一.知识点 ng-show.ng-hide.ng-if(控制元素显示隐藏,区别在于ng-hide是是否显示隐藏元素,而ng-if是是否移除元素): ng-src.ng-class(为元素添加路径和cl ...
- [Luogu 1160] 队列安排
Luogu 1160 队列安排 链表H2O H2O H2O模板. 太久不写链表,忘干净了,竟调了半个晚上. 保留备用. #include <cstdio> #include <cst ...
- 省队集训 Day3 杨北大
[题目大意] 给出平面上$n$个点$(x_i, y_i)$,请选择一个不在这$n$个点之内的点$(X, Y)$,定义$(X, Y)$的价值为往上下左右四个方向射出去直线,经过$n$个点中的数量的最小值 ...
- Zabbix 通过 JMX 监控 java 进程
参考: [ JMX monitoring ] [ Zabbix Java gateway ] [ JMX Monitoring (Java Gateway) not Working ] [ Monit ...
- UIImageView属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...
- 《Applying Deep Learning to Answer Selection: A Study And an Open Task》文章理解小结
本篇论文是2015年的IBM watson团队的. 论文地址: 这是一篇关于QA问题的一篇论文: 相关论文讲解1.https://www.jianshu.com/p/48024e9f7bb22.htt ...