HDU5487 Difference of Languages(BFS)
题意:给你两个自动机,求出最短的(如果有相同最短的则求出字典序最小的)能被其中一个自动机接收而不能被另外一个自动机接收的字符串。
一看是自动机以为是神题,后来比赛最后才有思路。
两个自动机的状态都是小于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)的更多相关文章
- HDU 5487 Difference of Languages(BFS)
HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...
- HDU 5487 Difference of Languages
Difference of Languages Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. ...
- 2015 ACM/ICPC Asia Regional Hefei Online
1001 Monitor the Alpacas 1002 The Relationship in Club 1003 Difference of Clustering 两边离散化.暴力扫C就过了. ...
- [转载]Maximum Flow: Augmenting Path Algorithms Comparison
https://www.topcoder.com/community/data-science/data-science-tutorials/maximum-flow-augmenting-path- ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- Difference Between XML and XAML.
XML, or Extensible Markup Language, is a subset of the more complex SGML (Standard Generalized Mark ...
- 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 ...
- Must practice programming questions in all languages
To master any programming languages, you need to definitely solve/practice the below-listed problems ...
- 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 ...
随机推荐
- PHP.23-ThinkPHP框架的三种模型实例化-(D()方法与M()方法的区别)
三种模型实例化 原则上:每个数据表应对应一个模型类(Home/Model/GoodsModel.class.php --> 表tp_goods) 1.直接实例化 和实例化其他类库一样实例化模型类 ...
- Javascript Step by Step - 01
基本数据类型 简单数值类型: undefined, null, boolean, number和string,共有5种 复合数据类型:object,array,function typeof操作符用来 ...
- Trident学习笔记(二)
aggregator ------------------ 聚合动作:聚合操作可以是基于batch.stream.partiton [聚合方式-分区聚合] partitionAggregate 分区聚 ...
- Parameter 'limit' not found. Available parameters are [arg1, arg0, pa
mybatis代码报错,这是因为mapper识别不了limit,需要替换成 LIMIT #{arg0},#{arg1}
- Springboot 启动问题
每次以debug方式启动springboot之后都会在SilentExitExceptionHandler类中的throw new SilentExitException() 解决办法 :window ...
- 3dMax,Maya与FBX
3DMax下载地址(包含安装教程与注册方法):http://www.3d66.com/popsoft_1.html 3DMax已经自带导出为fbx格式的功能,所以无需安装fbx插件 Maya下载地址( ...
- 解决使用Oracle数据库,项目启动由于表原因无法成功启动问题
1.仔细看异常信息,如果出现一个 翻译过来是 不仅仅这一张表,那就说明,在连接数据库,定位到表的时候有多张表,不知道连哪一张. 原因: 有多个用户,这两个用户下有相同的表. 就算是在不同的表空间也不 ...
- UnitOfWork知多少 【转】
原文链接:https://www.cnblogs.com/sheng-jie/p/7416302.html 1. 引言 Maintains a list of objects affected by ...
- vue零碎收集
在组件中创建dom: let a=document.querySelector('.test'); let newDom=document.createElement("div") ...
- 创建虚拟机流程nova
这篇博文借鉴于http://www.cnblogs.com/yjbjingcha/p/6977741.html,感谢博友提供. 本文试图具体地描写叙述openstack创建虚拟机的完整过程.从用户发起 ...