HDU 5487 Difference of Languages

寻找两个有限自动机代表语言的差异
本文探讨了如何通过构建有限自动机(DFA)来表示正则语言,并提出了一个方法来找出两个不同DFA所代表语言之间的差异。通过输入测试案例,程序能够输出一个字符串,该字符串被一个DFA接受但不被另一个DFA接受。

Difference of Languages

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on  HDU. Original ID: 5487
64-bit integer IO format: %I64d      Java class name: Main

A regular language can be represented as a deterministic finite automaton (DFA). A DFA contains a finite set of states Q and a finite set of input symbols called the alphabet Σ. Initially, the DFA is positioned at the start state q0∈Q. Given the transition function δ(q,a) and an input symbol a, the DFA transit to state δ(q,a) if its current state is q.
Let w=a1a2…an be a string over the alphabet Σ. According to the above definition, the DFA transits through the following sequence of states.
q0,q1=δ(q0,a1),q2=δ(q1,a2),…,qn=δ(qn−1,an)

The DFA also contains a set of accept states F⊆Q. If the last state qn is an accept state, we say that the DFA accepts the string w. The set of accepted strings is referred as the language that the DFA represents.
You are given two DFAs, and the languages that they represent may be different. You want to find the difference between the two languages. Specifically, you are trying to find a string that is accepted by one DFA but not accepted by the other DFA. As there could be multiple such strings, you only want the shortest one. If there are still multiple such strings, you would like the smallest one in lexicographical order.

Input
The first line of input contains a number T indicating the number of test cases (T≤200).
Each test case contains the description of two DFAs.
For the first DFA, the first line contains three integers N, M, and K, indicating the number of states, the number of rules describing the transition function, and the number of accept states (1≤K≤N≤1000,0≤M≤26N). The states are numbered from 0 to N–1. The start state is always 0.
The second line contains K integers representing the accept states. All these numbers are distinct.
Each of the next M lines consists of two states p and q, and an input symbol a, which means that the DFA transits from p to q when it receives the symbol a. You may assume that the alphabet in consideration consists of the 26 lowercase letters (a-z). It is guaranteed that, given p and a, the next state q is unique.
The description of the second DFA follows the same format as the above.

Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the shortest string that is accepted by one DFA but not accepted by the other DFA. If no such string exists, output the digit “0” instead. Note that an empty string is also considered a string.

Sample Input

2
3 3 1
2
0 1 a
1 2 b
2 0 c
4 4 1
3
0 1 a
1 2 b
2 3 c
3 0 a
3 3 1
2
0 1 a
1 2 b
2 0 c
3 4 1
2
0 1 a
1 2 b
1 2 c
2 0 c

Sample Output

Case #1: ab
Case #2: ac

Source

 
解题:直接搜啊,根据双方的转移状态进行转移,当能够被一个AC被另一个不能AC说明找到了,因为是bfs优先搜索字典序小的,所以自然是和题意的
 1 #include <bits/stdc++.h>
 2 #define st first
 3 #define nd second
 4 using namespace std;
 5 using PII = pair<int,int>;
 6 const int maxn = 1002;
 7 struct Trie {
 8     int son[maxn<<1][26],n;
 9     bool ac[maxn<<1];
10     void init(int n) {
11         memset(son,-1,sizeof son);
12         memset(ac,false,sizeof ac);
13         this->n = n;
14     }
15     void add(int u,int v,char ch) {
16         son[u][ch - 'a'] = v;
17     }
18 } A,B;
19 queue<PII>q;
20 char str[maxn];
21 int d[maxn][maxn],P[maxn][maxn],cnt;
22 PII Pre[maxn][maxn];
23 bool bfs() {
24     while(!q.empty()) q.pop();
25     q.push(PII(0,0));
26     cnt += maxn;
27     d[0][0] = cnt;
28     while(!q.empty()) {
29         PII now = q.front();
30         q.pop();
31         PII x = now;
32         if(A.ac[now.st]^B.ac[now.nd]){
33             str[d[now.st][now.nd] - cnt] = 0;
34             for(int i = 0,len = d[now.st][now.nd] - cnt; i < len; ++i){
35                 str[len - i - 1] = P[x.st][x.nd] + 'a';
36                 x = Pre[x.st][x.nd];
37             }
38             return true;
39         }
40         for(int i = 0; i < 26; ++i){
41             x.st = A.son[now.st][i] == -1?A.n:A.son[now.st][i];
42             x.nd = B.son[now.nd][i] == -1?B.n:B.son[now.nd][i];
43             if(d[x.st][x.nd] >= cnt) continue;
44             d[x.st][x.nd] = d[now.st][now.nd] + 1;
45             Pre[x.st][x.nd] = now;
46             P[x.st][x.nd] = i;
47             q.push(x);
48         }
49     }
50     return false;
51 }
52 int main() {
53     int kase,n,m,k,u,v,cs = 1;
54     scanf("%d",&kase);
55     while(kase--) {
56         scanf("%d%d%d",&n,&m,&k);
57         A.init(n);
58         while(k--) {
59             scanf("%d",&u);
60             A.ac[u] = true;
61         }
62         while(m--) {
63             scanf("%d%d%s",&u,&v,str);
64             A.add(u,v,str[0]);
65         }
66         scanf("%d%d%d",&n,&m,&k);
67         B.init(n);
68         while(k--) {
69             scanf("%d",&u);
70             B.ac[u] = true;
71         }
72         while(m--) {
73             scanf("%d%d%s",&u,&v,str);
74             B.add(u,v,str[0]);
75         }
76         if(bfs()) printf("Case #%d: %s\n",cs++,str);
77         else printf("Case #%d: 0\n",cs++);
78     }
79     return 0;
80 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4913485.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值