这个是最基础的解释器,它实现了串联、并联、克林闭包,字符集为除了()|*的ASCII字符,而且不能判断表达式合法,效率还很低,内存利用率低。

它只能判读输入的字符串是否符合表达式。

#include<bits/stdc++.h>
using namespace std;
namespace GDRegex
{
const int EPSILON=-;
const int ISEND=-;
const int MAXN=<<;
struct Node;
struct Edge;
struct Fa;
int TI,c1[MAXN],c2[MAXN];
struct Edge
{
Edge*next;
Node*to;
int w;
};
struct Node
{
Edge*head,*tail;
int num;
Node()
{
num=++TI;
head=new Edge;
head->w=ISEND;
tail=head;
}
inline void add(Node*v,int w)
{
assert(w!=ISEND);
tail->to=v;
tail->w=w;
tail->next=new Edge;
tail=tail->next;
tail->w=ISEND;
}
}*wait1[MAXN],*wait2[MAXN];
struct FA
{
Node*start,*end;
int endPos;
FA()
{
start=new Node();
end=new Node();
start->add(end,EPSILON);
endPos=end->num;
};
FA(int ch)
{
start=new Node();
end=new Node();
start->add(end,ch);
endPos=end->num;
}
FA(string str)
{
start=new Node();
end=new Node();
Node*p=start;
for(int i=;i<str.size();++i)
{
Node*x=new Node;
p->add(x,str[i]);
p=x;
}
p->add(end,EPSILON);
endPos=end->num;
}
bool match(string str)
{
int tot=;
map<pair<int,int>,bool>vis;
int len1=,len2=,len=str.size();
c1[len1]=;
wait1[len1++]=start;
vis[make_pair(,start->num)]=;
while(len1)
{
len2=;
for(int i=;i<len1;++i)
{
Node*u=wait1[i];
for(Edge*e=u->head;e->w!=ISEND;e=e->next)
{
Node*v=e->to;
// cout<<"NOW "<<u->num<<" "<<v->num<<" "<<(char)e->w<<" "<<str[c1[i]]<<" "<<c1[i]<<endl;
++tot;
if(e->w==EPSILON)
{
if(c1[i]==len&&v->num==endPos)
return true;
if(vis[make_pair(v->num,c1[i])])
continue;
c2[len2]=c1[i];
wait2[len2]=v;
vis[make_pair(v->num,c2[len2++])]=;
}
else if(e->w==str[c1[i]]&&!vis[make_pair(v->num,c1[i]+)])
{
if(c1[i]+==len&&v->num==endPos)
return true;
if(c1[i]+>len)
continue;
c2[len2]=c1[i]+;
wait2[len2]=v;
vis[make_pair(v->num,c2[len2++])]=;
}
}
}
for(int i=;i<max(len1,len2);++i)
swap(wait1[i],wait2[i]),swap(c1[i],c2[i]);
swap(len1,len2);
}
return false;
}
};
FA getSeries(const FA&A,const FA&B)
{
FA C=A;
C.end->add(B.start,EPSILON);
C.end=B.end;
C.endPos=B.endPos;
return C;
}
FA getParallel(const FA&A,const FA&B)
{
FA C,D=A,E=B;
C.end->add(D.start,EPSILON);
C.end->add(E.start,EPSILON);
D.end->add(E.end,EPSILON);
C.end=E.end;
C.endPos=E.endPos;
return C;
}
FA getKleene(const FA&A)
{
FA B,C=A;
B.start->add(C.start,EPSILON);
C.end->add(B.start,EPSILON);
return B;
}
FA getFA(string str)
{
stack<pair<FA,int> >S;
for(int i=;i<str.size();++i)
{
if(str[i]=='(')
S.push(make_pair(FA(),));
else if(str[i]==')')
{
pair<FA,int>A=S.top();
S.pop();
while(!S.empty())
{
pair<FA,int>B=S.top();
if(B.second==)
break;
else if(B.second==)
A.first=getSeries(B.first,A.first);
else if(B.second==)
A.first=getParallel(B.first,A.first);
S.pop();
}
A.second=;
S.push(A);
}
else if(str[i]=='*')
{
pair<FA,int>A=S.top();
S.pop();
A.first=getKleene(A.first);
A.second=;
S.push(A);
}
else if(str[i]=='|')
{
pair<FA,int>A=S.top();
S.pop();
while(!S.empty())
{
pair<FA,int>B=S.top();
if(B.second==||B.second==)
break;
A.first=getSeries(B.first,A.first);
S.pop();
}
A.second=;
S.push(A);
}
else
S.push(make_pair(FA(str[i]),));
}
FA A;
while(!S.empty())
{
pair<FA,int>B=S.top();
S.pop();
if(B.second==)
A=getSeries(B.first,A);
else if(B.second==)
A=getParallel(B.first,A);
}
return A;
}
}
int main()
{
ios::sync_with_stdio(false);
using namespace GDRegex;
FA A=getFA("(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)");
while(true)
{
string str;
cin>>str;
cout<<A.match(str)<<endl;
}
return ;
}

2020_02_01

正则表达式在c++中的实现的更多相关文章

  1. 正则表达式在iOS中的运用

    1.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  2. 正则表达式(/[^0-9]/g,'')中的"/g"是什么意思?

    解答“正则表达式(/[^0-9]/g,'')中的"/g"是什么意思?”这个问题,也为了能够便于大家对正则表达式有一个更为综合和深刻的认识,我将一些关键点和容易犯糊涂的地方再系统总结 ...

  3. 正则表达式(/[^0-9]/g,'')中的"/g"是什么意思 ?

    正则表达式(/[^0-9]/g,'')中的"/g"是什么意思 ?     表达式加上参数g之后,表明可以进行全局匹配,注意这里“可以”的含义.我们详细叙述: 1)对于表达式对象的e ...

  4. JS正则表达式获取字符串中特定字符

    JS正则表达式获取字符串中得特定字符,通过replace的回调函数获取. 实现的效果:在字符串中abcdefgname='test'sddfhskshjsfsjdfps中获取name的值test  实 ...

  5. C#用正则表达式去掉Html中的script脚本和html标签

    原文 C#用正则表达式去掉Html中的script脚本和html标签 /// <summary>         /// 用正则表达式去掉Html中的script脚本和html标签     ...

  6. java正则表达式提取地址中的ip和端口号

    由于我需要用到java正则表达式提取地址中的ip和端口号,所以我就写了一个demo,测试一下,下面是demo public class Test0810_1 { public static void ...

  7. 使用Dreamweaver正则表达式替换href中的内容

    在Dreamweaver中使用正则表达式替换href中的内容,就像下面这些href中的内容复杂多样的情况下,href="/html/u.html",href="/tuho ...

  8. 正则表达式在Java中的使用

    目录 介绍 从简单例子认识正则表达式匹配 Java中对正则表达式的支持(各种语言有相应的实现) 初步认识 . + * ? 范围 认识\s \w \d - 下面介绍数字和字母的正则表达, 这是编程中使用 ...

  9. 正则表达式与Python中re模块的使用

    正则表达式与Python中re模块的使用 最近做了点爬虫,正则表达式使用的非常多,用Python做的话会用到re模块. 本文总结一下正则表达式与re模块的基础与使用. 另外,给大家介绍一个在线测试正则 ...

  10. 正则表达式在JS中的使用

    <script type="text/javascript"> /** *正则表达式在js中的第一种使用方式: * RegExp 通过构造器去使用正则表达式 需要对反斜 ...

随机推荐

  1. CP防火墙备份与还原

    Step1:进入专家模式 ====================================================== 如果没有设置专家模式的密码,执行下面命令进行设置: BJ-OFF ...

  2. 聚类分析 一、k-means

    前言 人们常说"物以类聚,人以群分",在生物学中也对生物从界门纲目科属种中进行了划分.在统计学中,也有聚类分析法,通过把相似的对象通过静态分类的方法分成不同的组别或者更多的子集,从 ...

  3. jquery中为动态增加的元素添加事件

    // html代码 <ul id="main"> </ul> // js代码 $(function(){ // 动态添加html代码 $("#ma ...

  4. nginx部署vue跨域proxy方式

    server { listen 80; charset utf-8; #server_name localhost; server_name you_h5_name; ###VUE项目H5域名 err ...

  5. 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。-----力扣

    给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...

  6. vue 源码 学习days8-比较两个对象的方法

    // 在面试中可能会遇到, 思想重要 // 比较两个对象是否是相等的 两个对象 // 1. js 中对象是无法使用 == 来比较的, 比是地址 // 2. 我们一般会定义如果对象的各个属性值都相等 那 ...

  7. 6.7 Mapreduce作业流JobControl和Oozie

    1.1  Mapreduce作业流JobControl和Oozie 更复杂的任务,需要多个mapreduce作业,形成作业流,而不是增加map和reduce的复杂度.复杂问题,可以用高级语言pig.h ...

  8. schedule of 2016-09-26~2016-10-02(Monday~Sunday)——1st semester of 2nd Grade

    2016/9/26 Monday 1.make ppt for this afternoon's group meeting 2.ask teacher Xiqi&Liu some probl ...

  9. ACM北大暑期课培训第七天

    昨天没时间写,今天补下. 昨天学的强连通分支,桥和割点,基本的网络流算法以及Dinic算法: 强连通分支 定义:在有向图G中,如果任意两个不同的顶点 相互可达,则称该有向图是强连通的. 有向图G的极大 ...

  10. Python在Windows下列出所有的安装包和模块

    1.查看python安装的module python -m pydoc module 或 >>>help('module') 2.用pip查看 pip list