题意

输入两个正规表达式,判断两者是否相交(即存在一个串同时满足两个正规表达式)。本题的正规表达式包含如下几种情况:

  • 单个小写字符 $c$
  • 或:($P | Q$). 如果字符串 $s$ 满足 $P$ 或者满足 $Q$,则 $s$ 满足 $(P| Q)$
  • 连接:($PQ$). 如果字符串 $s_1$ 满足 $P$,$s_2$ 满足 $Q$,则 $s_1s_2$ 满足 $(PQ)$
  • 克莱因闭包:$(P^*)$. 如果字符串 $s$ 可以写成0个或多个字符串 $s_i$ 的连接 $s_1s_2...$,且每个串都满足 $P$,则 $s$ 满足 $(P^*)$。注意,空串也满足 $(P^*)$

分析

先把每种情况都转成自动机,正则表达式也就是这些自动机的组合。都转成 NFA,再使用DFS或BFS寻找一个同时被两个自动机接受的非空串。

码力不够啊(平常的模拟题都是交给队友做的),下面给出 lrj 的代码,%%%。

(好像UVa上这题数据错了,已经两年没人AC。

// UVa1672 Disjoint Regular Expressions
// Rujia Liu
//
// This is Problem 12-2 of <<Beginning Algorithm Contests>> 2nd edition
//
// This code is neither simplest nor most efficient, but it's easy to understand and fast enough.
// Algorithm implemented here:
// 1. build epsilon-NFA from the regex
// 2. build NFA by removing epsilon from epsilon-NFA. Note that we did NOT optimize the epsilon-NFA as described in the book.
// 3. use BFS to find a common string of these two NFAs
// Attention: the output should NOT be empty so we used a little trick.
//
// Alternative algorithm: do BFS directly on epsilon-NFAs.
// State is (s1,s2,b) where b=1 iff at least one non-epsilon transition is performed.
// However, this graph is now 0-1 weighted so we need to use deque (or two-phase BFS).
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<string>
#include<queue>
#include<cassert>
#define REP(i,n) for(int i = 0; i < (n); ++i) using namespace std; // Part I: Expression Parser
struct ExprNode {
enum {A, STAR, OR, CONCAT};
int type, val;
ExprNode *l, *r; ExprNode(int type, ExprNode* l, ExprNode* r, int val = -):type(type),l(l),r(r),val(val){}
~ExprNode() {
if(l) delete l;
if(r) delete r;
}
}; struct Parser {
char* s;
int p, n; void Skip(char c) { p++; } // for debug purpose // (u)*
ExprNode* Item() {
ExprNode* u;
if(s[p] == '(') { Skip('('); u = Expr(); Skip(')'); }
else u = new ExprNode(ExprNode::A, NULL, NULL, s[p++]);
while(s[p] == '*') {
Skip('*');
u = new ExprNode(ExprNode::STAR, u, NULL);
}
return u;
} // u1u2u3...
ExprNode* Concat() {
ExprNode* u = Item();
while(s[p] && s[p] != ')' && s[p] != '|')
u = new ExprNode(ExprNode::CONCAT, u, Item());
return u;
} // u1|u2|u3
ExprNode* Expr() {
ExprNode* u = Concat();
while(s[p] == '|') {
Skip('|');
u = new ExprNode(ExprNode::OR, u, Concat());
}
return u;
} ExprNode* parse(char* str) {
s = str;
n = strlen(s);
p = ;
return Expr();
} }; // Part II: NFA construction
const int maxs = * + ; struct NFA {
int n; // number of states struct Transition {
int ch, next;
Transition(int ch = , int next = ):ch(ch),next(next){}
bool operator < (const Transition& rhs) const {
if(ch != rhs.ch) return ch < rhs.ch;
return next < rhs.next;
}
};
vector<Transition> trans[maxs]; void add(int s, int t, int c) {
trans[s].push_back(Transition(c, t));
} void process(ExprNode* u) {
int st = n++; // state 'start'
if(u->type == ExprNode::A) add(st, n, u->val);
else if(u->type == ExprNode::STAR) {
process(u->l);
add(st, st+, -);
add(st, n, -);
add(n-, st, -);
}
else if(u->type == ExprNode::OR) {
process(u->l);
int m = n;
process(u->r);
add(st, st+, -);
add(st, m, -);
add(m-, n, -);
add(n-, n, -);
}
else if(u->type == ExprNode::CONCAT) {
add(st, st+, -);
process(u->l);
add(n-, n, -);
process(u->r);
add(n-, n, -);
}
n++; // state 'end'
} void init(char* s) {
Parser p;
ExprNode* root = p.parse(s);
n = ;
for(int i = ; i < maxs; i++) {
trans[i].clear();
}
process(root);
delete root;
} vector<int> ss; // starting states void remove_epsilon() {
// find epsilon-closure for each state
vector<int> reachable[maxs];
int vis[maxs];
for(int i = ; i < n; i++) {
reachable[i].clear();
reachable[i].push_back(i);
queue<int> q;
q.push(i);
memset(vis, , sizeof(vis));
vis[i] = ;
while(!q.empty()) {
int s = q.front(); q.pop();
for(int j = ; j < trans[s].size(); j++)
if(trans[s][j].ch == -) {
int s2 = trans[s][j].next;
if(!vis[s2]) {
reachable[i].push_back(s2);
vis[s2] = ;
q.push(s2);
}
}
}
}
ss = reachable[]; // merge transitions
for(int i = ; i < n; i++) {
set<Transition> tr;
for(int j = ; j < trans[i].size(); j++) {
if(trans[i][j].ch == -) continue;
int s = trans[i][j].next;
for(int k = ; k < reachable[s].size(); k++)
tr.insert(Transition(trans[i][j].ch, reachable[s][k]));
}
trans[i] = vector<Transition>(tr.begin(), tr.end());
}
}
}; // Part III: BFS to find the answer const int maxn = + ;
const int maxq = * * * * + ; // case 26
char sa[maxn], sb[maxn]; struct State {
int s1, s2, fa, ch;
} states[maxq];
int ns; void print_solution(int s) {
if(states[s].fa == -) return;
print_solution(states[s].fa);
printf("%c", states[s].ch);
} void solve(const NFA& A, const NFA& B) {
queue<int> q;
int vis[maxs][maxs];
memset(vis, , sizeof(vis));
ns = ;
REP(i, A.ss.size())
REP(j, B.ss.size()) {
int s1 = A.ss[i], s2 = B.ss[j];
states[ns].s1 = s1;
states[ns].s2 = s2;
states[ns].fa = -;
q.push(ns++);
} while(!q.empty()) {
int s = q.front(); q.pop();
int s1 = states[s].s1;
int s2 = states[s].s2;
if(s1 == A.n- && s2 == B.n- && states[s].fa != -) {
printf("Wrong\n");
print_solution(s);
printf("\n");
return;
}
int n1 = A.trans[s1].size();
int n2 = B.trans[s2].size(); REP(i, n1) REP(j, n2)
if(A.trans[s1][i].ch == B.trans[s2][j].ch) {
int s1b = A.trans[s1][i].next;
int s2b = B.trans[s2][j].next;
int c = A.trans[s1][i].ch;
if(vis[s1b][s2b]) continue;
vis[s1b][s2b] = ;
states[ns].s1 = s1b;
states[ns].s2 = s2b;
states[ns].fa = s;
states[ns].ch = c;
q.push(ns++);
}
}
printf("Correct\n");
} NFA A, B;
int main() {
while(scanf("%s%s", sa, sb) == ) {
A.init(sa);
B.init(sb);
A.remove_epsilon();
B.remove_epsilon();
solve(A, B);
}
return ;
}

l

UVA 1672不相交的正规表达式的更多相关文章

  1. 学JS的心路历程-正规表达式Regular Expression

    今天我们来看正规表达式,在谈到为什么需要多学这个之前,先来看个示例. 假设需要判断输入字串是否含有“apple”: var text=“A apple a day keeps the doctor a ...

  2. 正规表达式 转 NFA C++

    今天来为大家分享一个编译原理中用正规表达式转NFA的小程序 正规表达式就是类似正则一样的式子,例如:(a|b)*abb,最后应该转化为: 大致的处理流程为: 例子中的表达式:(a|b)*abb,|和* ...

  3. 1.java实现——正规表达式判断

    目标:这个代码仅局限于所展示的正规表达式判断,也就是这是一个较单一的正规表达式判断(简易版). 既然是简易版的,所以若要修改这个正规表达式也是非常容易的,只要将二维数组中的数组修改即可.数组数据依据, ...

  4. 正规表达式与有限自动机和LEX

    正规式与有限自动机的等价性 一个正规式r与一个有限自动机M等价, L(r)=L(M) FA ->正规式,对任何FA M,都存在一个正规式r,使得L(r)=L(M). 正规式 -> FA, ...

  5. Linux学习十七、正规表达式练习题

    情境模拟题一:透过 grep 搜寻特殊字串,并配合数据流重导向来处理大量的文件搜寻问题. 目标:正确的使用正规表示法: 前提:需要了解数据流重导向,以及透过子命令 $(command) 来处理档名的搜 ...

  6. UVA 1661 Equation (后缀表达式,表达式树,模拟,实现)

    题意:给出一个后缀表达式f(x),最多出现一次x,解方程f(x) = 0. 读取的时候用一个栈保存之前的结点,可以得到一颗二叉树,标记出现'X'的路径,先把没有出现'X'的子树算完,由于读取建树的时候 ...

  7. 轻松学Shell之认识正规表达式

    离线下载观看:http://down.51cto.com/data/148117   650) this.width=650;" onclick='window.open("htt ...

  8. vs 2017 正规表达式替换整行多行数据

    ((<OutputFile>..*</OutputFile>)[\S\s])[\S\s] 从 <OutputFile> 开始 到 </OutputFile&g ...

  9. Swift中用正规表达式判断String是否是手机号码

    func isTelNumber(num:NSString)->Bool { var mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$&qu ...

随机推荐

  1. strings包 — 汇总

    转自:https://www.jb51.net/article/148388.htm strings 包中的函数和方法 // Count 计算字符串 sep 在 s 中的非重叠个数 // 如果 sep ...

  2. AS3灰色图像

    一开始觉得AS3的滤镜很难使用,尤其是那些矩阵,让人望而生畏.最近写一个聊天模块,要用到离线状态下的灰色头像,于是认真研究了ColorMatrixFilter,发现其实也没有那么难.所谓的矩阵其实就是 ...

  3. spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件

    spring cloud  读取 配置文件属性值 1.bean @Data public class LocalFileConfig { /** * 文件存储地址 */ private String ...

  4. mysql 字符

    只适用mysql5.0以上的版本: 1.一个汉字占多少长度与编码有关:         UTF-8:一个汉字=3个字节            GBK:一个汉字=2个字节 2.varchar(n)表示n ...

  5. PMM--简介与部署

    一.PMM架构 Percona Monitoring and Management是percona一款开源的用于管理和监控MySQL 和MongoDB性能的开源平台,通过PMM客户端收集到的DB监控数 ...

  6. Spark 系列(十二)—— Spark SQL JOIN 操作

    一. 数据准备 本文主要介绍 Spark SQL 的多表连接,需要预先准备测试数据.分别创建员工和部门的 Datafame,并注册为临时视图,代码如下: val spark = SparkSessio ...

  7. logback 日志相关

    日志相关: logback:https://www.cnblogs.com/gavincoder/p/10091757.html https://www.cnblogs.com/hhhshct/p/9 ...

  8. 在 WPF 程序中应用 Windows 10 真?亚克力效果

    原文:在 WPF 程序中应用 Windows 10 真?亚克力效果 从 Windows 10 (1803) 开始,Win32 应用也可以有 API 来实现原生的亚克力效果了.不过相比于 UWP 来说, ...

  9. DDL和DML 的区别

    DDL (Data Definition Language 数据定义语言) create table 创建表 alter table 修改表 drop table 删除表 truncate table ...

  10. PHP敏感信息脱敏函数

    测试 $name = '王刚'; $mobile = '13817558198'; $name = desensitize($name,1,1); $mobile = desensitize($mob ...