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个方向. 第一维代表在 ...
随机推荐
- hdu_5748_Bellovin(LIS)
题目链接:hdu_5748_Bellovin 题意: 给你一个数列ai,设f(a1,a2,a3,..an)=(f1,f2,f3,...,fn),其中fi表示以ai结尾的最长递增子序列长度,注意:必须要 ...
- hdu_4823_Energy Conversion
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4823 题意:中文题,很清楚,要注意的是乘起来会爆int 题解: #include<cstdio& ...
- hud 2549 壮志难酬
Problem Description 话说MCA山上各路豪杰均出山抗敌,去年曾在江湖威名显赫的,江湖人称<万军中取上将首级舍我其谁>的甘露也不甘示弱,“天将降大任于斯人也,必先劳其筋骨, ...
- sql server 2000/2005递归
/* 递归查询 塗聚文---SQL Server 2005环境下的实现: */--生成测试数据 create table Dept(ID int,ParentID int,msg varchar(20 ...
- Javascript教程
Javascript教程 laiqun@msn.cn Contents 1. javascript嵌入方式 2. javascript语法 3. 数据类型 4. javascript变量 5. 字符串 ...
- JSP基本语法--Page指令 <%@page 属性=”内容“%>
page指令语法:<%@page 属性=”内容“%> 常用:contentType,import,pageEncoding 例子,设置MIME属性,如果使用一些高版本的tomcat,可能自 ...
- Explain of Interaction Operators in UML?
来源于:EA 中的 Interaction Operators Enterprise Architect User Guide Operator Action alt Divide up intera ...
- 免费 WebOffice使用
目前WebOffice使用比较多主要有两个公司的产品,分别是江西金格和北京点聚.但是点聚的是免费的,虽然有欠缺之处,但是经过个人修改还是比较好用的,关键一点是,它免费啊! 把一个最主要加载页面,如果读 ...
- Java学习笔记之自定义异常
1.自定义异常类: /** * 自定义异常,只要继承继承Exception类或其子类即可 * @author Administrator * */ public class FileException ...
- How To Add Swap on Ubuntu 14.04
https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 How To Add Swap on ...