DFA Minimization
有点晚了我就不调试了..不过说是这么说我还是过了编译的..
#include<bits/stdc++.h>
using namespace std;
namespace DFA{
const int alp=10;
struct Node{int t[alp],a;Node(){}Node(int tx,int ac):a(ac){for(int i=0;i<alp;++i)t[i]=tx;}};
struct DFA{
vector<Node>nodes;
int starting;
int crt(int ac=0){nodes.emplace_back(nodes.size(),ac);}
void ss(int s){starting=s;}
void st(int s,int c,int t){nodes[s].t[c]=t;}
void sa(int s,int a){nodes[s].a=a;}
};
int _C_u_dd(vector<int>&a,const DFA&tt,int pos,int up){
a[pos]=up++;
const Node&g=tt.nodes[pos];
for(int i=0;i<alp;++i)if(!a[g.t[i]])
up=_C_u_dd(a,tt,g.t[i],up);
return up;
}
int Cut_unreachable(DFA&cc,const DFA&tt){
cc.nodes.clear();
vector<int>z;
z.resize(tt.nodes.size());
int gr=_C_u_dd(z,tt,tt.starting,1);
cc.nodes.resize(gr);
for(int i=0,_=tt.nodes.size();i<_;++i)if(z[i]){
const Node&p=tt.nodes[i];
Node&q=cc.nodes[z[i]];
q.a=p.a;
for(int i=0;i<alp;++i)
q.t[i]=z[p.t[i]];
}cc.ss(1);
return gr-1;
}
namespace PRS{
struct q{
vector<int>son;int tid,in;
q(int sz=0):tid(-1){if(sz)son.resize(sz);}
};
typedef pair<int,int>o;
struct p{
vector<q>z;
vector<o>b;
p(int t){
z.emplace_back(t);
b.resize(t+1);
for(int i=0;i<t;++i)
b[i+1]=o(0,i),z[0].son[i]=i+1;
}
void _reassign(q&a,int s){
q&t=z[a.tid];
int id=a.son[s];
if(s!=a.son.size()-1){
int idr=a.son[a.son.size()-1];
b[idr].second=s;
swap(a.son[s],a.son[a.son.size()-1]);
}a.son.pop_back();
b[id]=o(a.tid,t.son.size());
t.son.push_back(id);
}
vector<o>split(vector<int>t){
vector<o>q;
for(int v:t){
if(z[b[v].first].tid==-1){
q.emplace_back(b[v].first,z.size());
z[b[v].first].tid=z.size(),z.emplace_back();
}_reassign(z[b[v].first],b[v].second);
}for(o g:q)
z[g.first].tid=-1;
return q;
}
};
}
typedef vector<int> inlist[alp];
void DFA_Minimize(DFA&tt){
DFA gg;
int si=Cut_unreachable(gg,tt);
PRS::p pr(si);
vector<int>vi;
inlist*inl=new inlist[si+1];
for(int i=1;i<=si;++i){
for(int j=0;j<alp;++j)
inl[gg.nodes[i].t[j]][j].push_back(i);
if(gg.nodes[i].a)
vi.push_back(i);
}auto gd=pr.split(vi)[0];
stack<int>sets;
sets.push(gd.second);
pr.z[gd.second].in=1;
while(!sets.empty()){
int setNow=sets.top();sets.pop();
pr.z[setNow].in=0;
vector<int>qs;
for(int i:pr.z[setNow].son)
qs.push_back(i);
for(int j=0;j<alp;++j){
vi.clear();
for(int i:qs)
for(int k:inl[i][j])
vi.push_back(k);
auto qt=pr.split(vi);
for(auto i:qt)
if(pr.z[i.first ].son.size()&&
pr.z[i.second].son.size())
if(pr.z[i.first ].in||
pr.z[i.first ].son.size()>
pr.z[i.second].son.size())
sets.push(i.second),pr.z[i.second].in=1;
else sets.push(i.first),pr.z[i.first].in=1;
}
}int id=1;
for(auto i:pr.z)if(i.son.size())
i.in=id++;
tt.nodes.resize(id);
for(auto i:pr.z)if(i.son.size()){
Node&q=tt.nodes[i.in];
Node&u=gg.nodes[i.son[0]];
for(int j=0;j<alp;++j)
q.t[j]=pr.z[pr.b[u.t[j]].first].in;
}tt.starting=pr.z[pr.b[1].first].in;
delete[]inl;
}
}
int main(){
return 0;
}
最后祝您身体健康,再见.
DFA Minimization的更多相关文章
- 基于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 实际上这张图片表示的是一个状态机,只是状态没有标出来.因为这 ...
- DFA敏感词过滤
import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.ArrayList; ...
- 求子串-KPM模式匹配-NFA/DFA
求子串 数据结构中对串的5种最小操作子集:串赋值,串比较,求串长,串连接,求子串,其他操作均可在该子集上实现 数据结构中串的模式匹配 KPM模式匹配算法 基本的模式匹配算法 //求字串subStrin ...
- 编译系统中的 NFA/DFA算法理解
1.问题概述 NFA 和 DFA浅析---要深入了解正则表达式,必须首先理解有穷自动机. 有穷自动机(Finite Automate)是用来模拟实物系统的数学模型,它包括如下五个部分: 有穷状态集St ...
随机推荐
- C#的接口基础教程之四 访问接口
对接口成员的访问 对接口方法的调用和采用索引指示器访问的规则与类中的情况也是相同的.如果底层成员的命名与继承而来的高层成员一致,那么底层成员将覆盖同名的高层成员.但由于接口支持多继承,在多继承中,如果 ...
- qsort()和bsearch()
qsort void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); Sort ...
- Ansible指令和常用模块使用
这里文章记录一下ansible的指令选项和常用的模块使用 ansible指令选项 -m:要执行的模块,默认为command -a:模块的参数 -u:ssh连接的用户名,默认用root,ansible. ...
- PHP代码中出现中文乱码怎么办?
header("Content-type:text/html;charset=utf-8"); //加上这个就OK //示例 : class WechatController ex ...
- thinkphp 分页的 实现 和样式 分享
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgoAAABlCAIAAACjnlykAAAI8UlEQVR4nO3bP2/bSBrH8eSQ5rq0eh ...
- 开源数据库中间件-MyCat
开源数据库中间件-MyCat产生的背景 如今随着互联网的发展,数据的量级也是成指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系型数据库已经无法满足快速查询与插入数据的需求.这 ...
- HDU:3336-Count the string(next数组理解)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
- [BZOJ1010]玩具装箱toy(斜率优化)
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- Markdowm语法学习
Markdowm语法学习 标题 一级标题 一级标题 #一级标题 二级标题 二级标题 ##二级标题 六级标题 六级标题 ######六级标题 引用 引用 >引用 代码块 if(i == 0) { ...
- pip install 报错 Could not fetch URL
Could not fetch URL https://pypi.python.org/simple/xxx/: There was a problem confirming the ssl cert ...