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优先搜索字典序小的,所以自然是和题意的
 #include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
using PII = pair<int,int>;
const int maxn = ;
struct Trie {
int son[maxn<<][],n;
bool ac[maxn<<];
void init(int n) {
memset(son,-,sizeof son);
memset(ac,false,sizeof ac);
this->n = n;
}
void add(int u,int v,char ch) {
son[u][ch - 'a'] = v;
}
} A,B;
queue<PII>q;
char str[maxn];
int d[maxn][maxn],P[maxn][maxn],cnt;
PII Pre[maxn][maxn];
bool bfs() {
while(!q.empty()) q.pop();
q.push(PII(,));
cnt += maxn;
d[][] = cnt;
while(!q.empty()) {
PII now = q.front();
q.pop();
PII x = now;
if(A.ac[now.st]^B.ac[now.nd]){
str[d[now.st][now.nd] - cnt] = ;
for(int i = ,len = d[now.st][now.nd] - cnt; i < len; ++i){
str[len - i - ] = P[x.st][x.nd] + 'a';
x = Pre[x.st][x.nd];
}
return true;
}
for(int i = ; i < ; ++i){
x.st = A.son[now.st][i] == -?A.n:A.son[now.st][i];
x.nd = B.son[now.nd][i] == -?B.n:B.son[now.nd][i];
if(d[x.st][x.nd] >= cnt) continue;
d[x.st][x.nd] = d[now.st][now.nd] + ;
Pre[x.st][x.nd] = now;
P[x.st][x.nd] = i;
q.push(x);
}
}
return false;
}
int main() {
int kase,n,m,k,u,v,cs = ;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%d",&n,&m,&k);
A.init(n);
while(k--) {
scanf("%d",&u);
A.ac[u] = true;
}
while(m--) {
scanf("%d%d%s",&u,&v,str);
A.add(u,v,str[]);
}
scanf("%d%d%d",&n,&m,&k);
B.init(n);
while(k--) {
scanf("%d",&u);
B.ac[u] = true;
}
while(m--) {
scanf("%d%d%s",&u,&v,str);
B.add(u,v,str[]);
}
if(bfs()) printf("Case #%d: %s\n",cs++,str);
else printf("Case #%d: 0\n",cs++);
}
return ;
}

HDU 5487 Difference of Languages的更多相关文章

  1. HDU 5487 Difference of Languages(BFS)

    HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...

  2. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

  3. HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

    Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. HDU 5486 Difference of Clustering 暴力模拟

    Difference of Clustering HDU - 5486 题意:有n个实体,新旧两种聚类算法,每种算法有很多聚类,在同一算法里,一个实体只属于一个聚类,然后有以下三种模式. 第一种分散, ...

  5. HDU 5486 Difference of Clustering 图论

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5486 题意: 给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操 ...

  6. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...

  7. HDU 4715 Difference Between Primes (打表)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  8. hdu 4715 Difference Between Primes (打表 枚举)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  9. HDU 5936 Difference(折半搜索(中途相遇法))

    http://acm.hdu.edu.cn/showproblem.php?pid=5936 题意: 定义了这样一种算法,现在给出x和k的值,问有多少个y是符合条件的. 思路: y最多只有10位,再多 ...

随机推荐

  1. Android利用tcpdump抓包,用wireshark分析包。

    1.前言 主要介绍在android手机上如何利用tcpdump抓包,用wireshark分析包. android tcpdump官网: http://www.androidtcpdump.com/ t ...

  2. PowerShell和Bash的介绍

    PowerShell是运行在windows平台的脚本,而Bash是运行在linux平台的脚本 现在bash能做的事情,PowerShell也能做,PowerShell的强大之处是它可以管理window ...

  3. Aappcloud 调到二级页面黑屏

    PartnerHead3.html 后面多了一个点

  4. 在windows下编译出linux可执行程序

    set GOARCH=amd64 set GOOS=linux go build xx.go 会生成一个没有后缀的xx二进制文件 将该文件放入linux系统某个文件夹下 赋予权限 chmod 777 ...

  5. poj3262 Protecting the Flowers

    思路: 简单贪心,每次选择性价比最高的. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

  6. jQuery选择器之属性筛选选择器

    在这么多属性选择器中[attr="value"]和[attr*="value"]是最实用的 [attr="value"]能帮我们定位不同类型 ...

  7. Java递归调用改成非递归

          在java语言中,使用递归调用时,如果过多的调用容易造成java.lang.StackOverflowError即栈溢出和程序执行过慢.这是一个潜在Bug和影响程序执行效率问题,需要谨慎使 ...

  8. 远程桌面连接windowsServer

    1.win+R 打开windows运行工具栏: 2.输入 mstsc ,确定: 3.登录设置: 计算机:目标服务器ip地址:用户名:管理员或者用户的用户名,例如:administrator:密码:账户 ...

  9. 洛谷 大牛分站 P1000 超级玛丽游戏

    题目背景 本题是洛谷的试机题目,可以帮助了解洛谷的使用. 建议完成本题目后继续尝试P1001.P1008. 题目描述 超级玛丽是一个非常经典的游戏.请你用字符画的形式输出超级玛丽中的一个场景. *** ...

  10. swift的static和class修饰符---What is the difference between static func and class func in Swift?

    Special Kinds of Methods Methods associated with a type rather than an instance of a type must be ma ...