题意:给你两个自动机,求出最短的(如果有相同最短的则求出字典序最小的)能被其中一个自动机接收而不能被另外一个自动机接收的字符串。

一看是自动机以为是神题,后来比赛最后才有思路。

两个自动机的状态都是小于1000的,所以可以建一个图,每个结点(u,v)表示当前处于自动机1的状态u和自动机2的状态v,然后相应的这些状态接收[a-z]的字符就会转移到下一个状态。然后从原点(0,0)开始广搜,搜到的第一个accpet[u]!=accept[v]的即是所求的状态。(处理的时候要给每个自动机加一个状态,用来表示失配的时候的情况,这个状态的所有后继都转移向自己,而且本身不是accpet状态,广搜即可)

#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std; #define maxn 1005 int n1, m1, k1;
int n2, m2, k2; bool tar1[maxn];
bool tar2[maxn]; int go1[maxn][26];
int go2[maxn][26]; int prestate[maxn*maxn];
char prechar[maxn*maxn]; bool vis[maxn*maxn]; bool check(int x)
{
return tar1[x / n2] != tar2[x%n2];
} int nextState(int x, int c)
{
return go1[x / n2][c] * n2 + go2[x%n2][c];
} int main()
{
int T; cin >> T; int ca = 0;
while (T--)
{
memset(go1, -1, sizeof(go1));
memset(go2, -1, sizeof(go2));
memset(tar1, 0, sizeof(tar1));
memset(tar2, 0, sizeof(tar2)); int accept;
int ui, vi;
char ci[3]; scanf("%d%d%d", &n1, &m1, &k1);
for (int i = 0; i < k1; ++i){
scanf("%d", &accept);
tar1[accept] = true;
}
for (int i = 0; i < m1; ++i){
scanf("%d%d%s", &ui, &vi, ci);
go1[ui][ci[0] - 'a'] = vi;
}
for (int i = 0; i <= n1; ++i){
for (int k = 0; k < 26; ++k){
if (go1[i][k] == -1) go1[i][k] = n1;
}
} scanf("%d%d%d", &n2, &m2, &k2);
for (int i = 0; i < k2; ++i){
scanf("%d", &accept);
tar2[accept] = true;
}
for (int i = 0; i < m2; ++i){
scanf("%d%d%s", &ui, &vi, ci);
go2[ui][ci[0] - 'a'] = vi;
}
for (int i = 0; i <= n2; ++i){
for (int k = 0; k < 26; ++k){
if (go2[i][k] == -1) go2[i][k] = n2;
}
}
++n1; ++n2; int ans = -1;
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(0);
vis[0] = true;
while (!Q.empty())
{
int state = Q.front(); Q.pop();
if (check(state)){
ans = state;
break;
}
for (int k = 0; k < 26; ++k){
int nstate = nextState(state, k);
if (!vis[nstate]){
Q.push(nstate);
vis[nstate] = true;
prestate[nstate] = state;
prechar[nstate] = k;
}
}
}
if (-1 == ans){
printf("Case #%d: 0\n", ++ca);
continue;
}
string ts;
while (ans != 0){
ts.push_back(char('a' + prechar[ans]));
ans = prestate[ans];
}
reverse(ts.begin(), ts.end());
printf("Case #%d: %s\n", ++ca, ts.c_str());
}
return 0;
}

HDU5487 Difference of Languages(BFS)的更多相关文章

  1. HDU 5487 Difference of Languages(BFS)

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

  2. HDU 5487 Difference of Languages

    Difference of Languages Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. ...

  3. 2015 ACM/ICPC Asia Regional Hefei Online

    1001 Monitor the Alpacas 1002 The Relationship in Club 1003 Difference of Clustering 两边离散化.暴力扫C就过了. ...

  4. [转载]Maximum Flow: Augmenting Path Algorithms Comparison

    https://www.topcoder.com/community/data-science/data-science-tutorials/maximum-flow-augmenting-path- ...

  5. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  6. Difference Between XML and XAML.

    XML, or Extensible Markup Language, is a subset  of the more complex SGML (Standard Generalized Mark ...

  7. Do we need other languages other than C and C++?

    There were hundreds of or thousands of programming languages created since the invention of computer ...

  8. Must practice programming questions in all languages

    To master any programming languages, you need to definitely solve/practice the below-listed problems ...

  9. Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)

    D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. 十五、mac 中登陆mysql忘记密码解决办法

    mac 中登陆mysql忘记密码解决办法 1.打开终端,输入命令:cd /usr/local/mysql/bin 2.mysql -uroot -p,用这条命令登陆时报错信息: 报错:Enter pa ...

  2. 5.Mongodb聚合

    聚合 aggregate 聚合(aggregate)主要用于计算数据,类似sql中的sum().avg() 语法 db.集合名称.aggregate([{管道:{表达式}}]) 1.管道 管道在Uni ...

  3. 页面引入外部字体ttf,如何提取所需要的ttf字体或者加载过慢的解决方法-1127更新

    最近几天编写手机端的页面之后,文中需要华文行楷字体,在网上下载后,引入到了自己的前端页面,以为没有什么事了,继续码代码 @font-face { font-family:huawen; src: ur ...

  4. 获取ubuntu中软件包的有用地址

    http://us.archive.ubuntu.com/ubuntu/pool/main/g/gettext/

  5. 《Cracking the Coding Interview》——第7章:数学和概率论——题目3

    2014-03-20 02:05 题目:给定笛卡尔二维平面上两条直线,判断它们是否相交. 解法:相交.重合.平行. 代码: // 7.3 Given two lines on the Cartesia ...

  6. 抓包工具 - Fiddler - (二)

    <转载自 miantest> 在上一篇中介绍了Fiddler的基本使用方法.通过上一篇的操作我们可以直接抓取浏览器的数据包.但在APP测试中,我们需要抓取手机APP上的数据包,应该怎么操作 ...

  7. Centos7中查看IP地址命令ifconfig无法识别如何处理

    问题描述: 在虚拟机中已安装好Centos7系统,查看IP地址使用命令ifconfig时,提示找不到此命令,使用ip addr命令则可查询当前系统的IP地址(如图1.2): 图1 图2 解决问题步骤: ...

  8. VS调试时不捕捉Exception

    在方法上添加 [DebuggerHidden] 这样,发生错误的时候,VS就不会捕捉到这个错误,否则,会在错误的地方中断. [DebuggerHidden] public void DeleteAll ...

  9. 异或值 xor

    题目描述 给出一个 N 个点的带权无向图,要求从 1 号点到 N 号点的一条路径,使得路径上的边 权异或值最大. 输入格式 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M ...

  10. Educational Codeforces Round 42 (Rated for Div. 2) B

    B. Students in Railway Carriage time limit per test 2 seconds memory limit per test 256 megabytes in ...