HDU 5487 Difference of Languages(BFS)
HDU 5487 Difference of Languages
这题从昨天下午2点开始做,到现在才AC了。感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能生巧吧。
BFS转移的是两个DFA的状态,用typedef pair<int,int> pi;map<pi,pi> pres; 两步储存前后状态的链接。
附上一组测坑数据:
/*
432
4 3 1
3
0 1 a
1 2 e
2 3 w
6 3 1
5
0 1 a
1 3 e
3 5 h
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int n1,m1,k1;
int n2,m2,k2;
const int maxn = ;
int ac1[maxn];
int ac2[maxn];
map<char,int> mp1;
map<int,char> mp2;
int g1[maxn][];
int g2[maxn][];
int vis[maxn][maxn];
typedef pair<int,int> pi;
map<pi,pi> pres;
int a[maxn][maxn];
int flag = ;
struct edge
{
int st1,st2;
};
edge ans;
void pre()
{
for(int i=;i<=;i++)
{
char c = 'a'+i-;
mp1[c]=i;
mp2[i]=c;
}
}
void inin()
{
for(int i=;i<maxn;i++)
{
ac1[i] = ;
ac2[i] = ;
}
memset(g1,-,sizeof(g1));
memset(g2,-,sizeof(g2));
memset(vis,,sizeof(vis));
memset(a,,sizeof(a));
pres.clear();
}
bool check(int u,int c)
{
if(ac1[u]!=ac2[c])
return false;
return true;
}
void bfs()
{
queue<edge> q;
edge cur;
q.push((edge){,});
ans = {,};
flag = ;
while(!q.empty())
{
cur = q.front();
q.pop();
if(!check(cur.st1,cur.st2))
{
ans = cur;
flag = ;
return;
}
for(int i=;i<=;i++)
{
int v1 = g1[cur.st1][i];
int v2 = g2[cur.st2][i];
if(!vis[v1][v2])
{
vis[v1][v2] = ;
q.push((edge){v1,v2});
pres[pi(v1,v2)] = make_pair(cur.st1,cur.st2);
a[v1][v2] = i;
}
}
}
}
int main()
{
pre();
int t;
int kase = ;
cin>>t;
while(t--)
{
int u,v,x;
char c;
inin();
cin>>n1>>m1>>k1;
for(int i=;i<=k1;i++)
{
scanf("%d",&x);
ac1[x] = ;
}
for(int i=;i<=m1;i++)
{
scanf("%d %d %c",&u,&v,&c);
g1[u][mp1[c]] = v;
}
cin>>n2>>m2>>k2;
for(int i=;i<=k2;i++)
{
scanf("%d",&x);
ac2[x] = ;
}
for(int i=;i<=m2;i++)
{
scanf("%d %d %c",&u,&v,&c);
g2[u][mp1[c]] = v;
}
g1[][] = ;
g2[][] = ;
for(int i=;i<=n1;i++) //下面两步是防止 最后ac1[u]和ac2[c]索引为负。
for(int j=;j<=;j++)
if(g1[i][j]==-) g1[i][j]=n1;
for(int i=;i<=n2;i++)
for(int j=;j<=;j++)
if(g2[i][j]==-) g2[i][j]=n2;
bfs();
printf("Case #%d: ",++kase);
if(!flag)
{
printf("0\n");
continue;
}
vector<int> buffer;
pi cur;
while(pres.count(pi(ans.st1,ans.st2))&&(ans.st1!=||ans.st2!=))
{
buffer.push_back(a[ans.st1][ans.st2]);
cur = pres[pi(ans.st1,ans.st2)];
ans.st1 = cur.first;
ans.st2 = cur.second;
}
reverse(buffer.begin(),buffer.end());
for(int i=;i<buffer.size();i++)
{
printf("%c",buffer[i]+'a'-);
}
printf("\n");
}
return ;
}
/*
432
4 3 1
3
0 1 a
1 2 e
2 3 w
6 3 1
5
0 1 a
1 3 e
3 5 h
*/
HDU 5487 Difference of Languages(BFS)的更多相关文章
- HDU 5487 Difference of Languages
Difference of Languages Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. ...
- HDU5487 Difference of Languages(BFS)
题意:给你两个自动机,求出最短的(如果有相同最短的则求出字典序最小的)能被其中一个自动机接收而不能被另外一个自动机接收的字符串. 一看是自动机以为是神题,后来比赛最后才有思路. 两个自动机的状态都是小 ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- hdu 4715 Difference Between Primes
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...
- hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...
随机推荐
- LeetCode OJ 337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- Mac Git 学习笔记
lapommedeMacBook-Pro:~ lapomme$ cd GitHub lapommedeMacBook-Pro:GitHub lapomme$ cd lapommedeMacBook-P ...
- 轻松创建nodejs服务器(1):一个简单nodejs服务器例子
这篇文章主要介绍了一个简单nodejs服务器例子,本文实现了一个简单的hello world例子,并展示如何运行这个服务器,需要的朋友可以参考下 我们先来实现一个简单的例子,hello world ...
- ios控件 UILabel
UILabel 的作用是显示文本 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; lab ...
- 百度前端面试题-类似slack的在线聊天室
别人国庆出去玩,我在家写代码的感觉也是很不错哒. 首先介绍一下技术架构吧! 使用了js框架:FFF,zepto,jquery,md5.min.js 前端框架:Bootstrap 后端:野狗,部分PHP ...
- System.IO命名空间,用于文件/流的处理。
主要类的介绍:1 Path类——静态实用类,用于处理路径名称.2 File类和FileInfo类● File —— 静态实用类,提供许多静态方法,用于移动.复制和删除文件.● FileInfo —— ...
- perl中调用cgi
来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698856.html 参考:http://www.willmaster.com/librar ...
- yum仅下载不安装
通常是使用yum来安装解决依赖包关系,如果有一台服务器没法连接外网或yum源没有设置,希望通过另一台服务器将这些RPM包下载下来,然后再去安装.那么怎么使用yum工具来下载RPM包呢? 使用yum 要 ...
- kill -0
http://unix.stackexchange.com/questions/169898/what-does-kill-0-do 检查有没有权限杀他
- 【转】4G手机打电话为什么会断网 4G上网和通话不能并存原因分析
与2G/3G相比,4G最大的特色就是提供了超过100Mbps的峰值速率,既然速度都可以秒掉20M的光纤固网了,那用来语音通话不就更是小菜一碟了吗?很遗憾,问题就出现在了这里. 由于目前的LTE网络(4 ...