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)的更多相关文章

  1. HDU 5487 Difference of Languages

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

  2. HDU5487 Difference of Languages(BFS)

    题意:给你两个自动机,求出最短的(如果有相同最短的则求出字典序最小的)能被其中一个自动机接收而不能被另外一个自动机接收的字符串. 一看是自动机以为是神题,后来比赛最后才有思路. 两个自动机的状态都是小 ...

  3. 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,最先 ...

  4. HDU 5876 关于补图的bfs

    1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...

  5. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. HDU(1175),连连看,BFS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...

  7. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  8. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

  9. hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...

随机推荐

  1. python3.5 + django1.9.1+mysql

    python3 对mysql 的驱动不再是mysqldb 具体步骤 : 1 安装依赖 pip install PyMySQL 2 修改配置 __init__.py import pymysql pym ...

  2. QQ 自动接收远程连接之关闭了远程桌面

    之前使用都好好的,后来就不知道怎么了突然就不行了,在另外一个远程桌面软件(向日葵)失效后,木有办法,查查查,终于查出来了,是我本机的时间服务停止了,导致我本机的时间和服务器时间不一致,所以连接不上.只 ...

  3. 在调试安卓系统的时候需要这个 ”adb disable-verity“

    在调试设备的时候.想要对文件进行读写 于是使用adb remount 出现提示. 请使用 ”adb  disable-verity“ 于是使用adb  disable-verity 的命令. 得到如下 ...

  4. Log4j 简介及初步应用

    使用2.5版本有问题,暂时没有解决,也许是JDK版本不兼容的原因.因此使用的是log4j-1.2.8.jar 1.三个组件 日志记录器  ——  Logger.输出目的地 —— Appender.输出 ...

  5. 转 Oracle12c/11个 Client安装出现"[INS-30131]"错误“请确保当前用户具有访问临时位置所需的权限”解决办法之完整版

    错误分析:安装时exe会自动解压到C:\Users\Administrator\AppData\Local\Temp再进行安装,当文件夹权限不足时就会拒绝安装程序的访问: 第一步:  在win+R输入 ...

  6. 3.1 cron表达式

    1.Cron在线生成网址:      http://cron.qqe2.com/   http://www.pdtools.net/tools/becron.jsp#cron 2.Cron 概要 3. ...

  7. js设置全局变量ajax中赋值

    js设置全局变量,在ajax中给予赋值赋值不上问题解决方案 方案一. //在全局或某个需要的函数内设置Ajax异步为false,也就是同步. $.ajaxSetup({async : false}); ...

  8. 拖动滚动条时某一处相对另一处固定不动(position:fixed)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  9. Android Matrix类以及ColorMatri

    引自:http://www.chinabaike.com/t/37396/2014/0624/2556217.html Android Matrix类以及ColorMatrix类详解 最近在系统学习了 ...

  10. - (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; 方法浅析

    转载自:http://blog.csdn.net/ronaldo_carry/article/details/49070119 将viewdidload里面的代码全部注释掉 - (void)viewD ...