hdu 5487 Difference of Languages BFS

Difference of Languages

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 526    Accepted Submission(s): 120


Problem Description
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  q0Q . 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=a1a2an  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=δ(qn1,an)

The DFA also contains a set of accept states  FQ . 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 ( T200 ).
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 ( 1KN1000,0M26N ). The states are numbered from 0 to  N1 . 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
 

题目:给两个DFA,求最小的串被一个自动机接受而不被另一个自动机接受。

最小:长度不一样取短的,长度一样取字典序小的

从(0,0)开始做BFS,(u,v)表示匹配到第一个自动及的u状态,第二个自动机的v状态。

转移有26种,所以是26*n1*n2的复杂度。 增加一个节点是不接受的串的状态,这样可以保证一个

自动及不接受,另一个可能接受


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 1002
struct Node{
    int ch[26];
};
Node dfa[2][maxn];
bool mark[2][maxn];

void getin(Node *df,bool mar[],int &n){
    int m,k,u,v;
    char x[2];
    scanf("%d%d%d",&n,&m,&k);
    for(int i = 0;i <= n; i++)
        for(int j = 0;j < 26; j++)
            df[i].ch[j] = n;
    for(int i = 0;i <= n; i++)
        mar[i] = 0;
    for(int i = 0;i < k; i++){
        scanf("%d",&u);
        mar[u] = 1;
    }
    for(int i = 0;i < m; i++){
        scanf("%d%d%s",&u,&v,x);
        df[u].ch[x[0]-'a'] = v;
    }
}

bool mp[maxn][maxn];
int pre[maxn][maxn],ans[maxn][maxn];
int queue[maxn*maxn];
void dfs(int s){
    int u = s % 2000;
    int v = s / 2000;
    if(u == 0 && v == 0) return ;
    dfs(pre[u][v]);
    printf("%c",ans[u][v]+'a');
}

void bfs(int n1,int n2){
    int tail = 1,top = 0;
    queue[0] = 0;
    for(int i = 0;i <= n1; i++)
        for(int j = 0;j <= n2; j++)
            mp[i][j] = 0;
    mp[0][0] = 1;
    int s,u,v,x,y,res=-1,z;
    if(mark[0][0] ^ mark[1][0]) res = 0;

    while(tail > top && res == -1){
        s = queue[top++];
        u = s % 2000;
        v = s / 2000;
        for(int i = 0;i < 26 && res == -1; i++){
            x = dfa[0][u].ch[i];
            y = dfa[1][v].ch[i];
            z = x + y * 2000;
            if(mark[0][x] ^ mark[1][y]) res = z;
            if(mp[x][y]) continue;
            pre[x][y] = s;
            ans[x][y] = i;
            mp[x][y] = 1;
            queue[tail++] = z;
        }
    }
    if(res == -1) printf("0");
    else dfs(res);
}

int main(){
    int t,tt=1,n1,n2,m,k,u,v;
    scanf("%d",&t);
    while(t--){
        getin(dfa[0],mark[0],n1);
        getin(dfa[1],mark[1],n2);
        printf("Case #%d: ",tt++);
        bfs(n1,n2);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值