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个方向. 第一维代表在 ...
随机推荐
- Git 多人协作开发
当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且你的远程仓库的默认名称是origin 查看远程库的信息,用git remote LV@LV-PC ...
- ILMerge 简单使用
ILMerge是合并.net的assembly的工具,最新版的支持.net 4.0的ILmerge下载: http://www.microsoft.com/downloads/details.aspx ...
- 用TLS实现安全TCP传输及配置和访问https的web服务(转)
tls相关 大致原理 为了让两个之间实现安全传输,(我们把服务端统一叫做TcpServer,客户端统一叫做TcpClient),TcpServer在listen完了accept之后要用一个证书来声明自 ...
- 编程实现Windows瞬间关机
我们先来看看Windows正常的关机流程:①关机指令通知Windows子系统csrss.exe,csrss.exe收到通知后会和Winlogon.exe做一个数据交换,再由Winlogon.exe通知 ...
- 转 awk 使用方法
Shell编程-awk 简介 awk 是一种对立的编程语言,集成于所有UNIX/Linux中,这个名字是它创建者的名字首字母组成的 Alfred Aho,Peter Weinberger, and B ...
- zTree异步加载并初始化树时全部展开(贴主要代码)
<%@page pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctyp ...
- java数据结构之有序表查找
这篇文章是关于有序表的查找,主要包括了顺序查找的优化用法.折半查找.插值查找.斐波那契查找: 顺序优化查找:效率极为底下,但是算法简单,适用于小型数据查找: 折半查找:又称为二分查找,它是从查找表的中 ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 更换arm-linux-gcc 4.3.2编译器
先创建一个临时目录:mcx@mcx-virtual-machine:/home/work/tools$ mkdir tmp 解压到根目录:mcx@mcx-virtual-machine:/home/w ...