HDU2471_History of Languages
有意思的题目,给出两个自动机,判断这个两个自动机是否是等价的?
两个自动机是等价的,那么他们可接受的字符串集合一定是完全一样的。
于是我们可以从(0,0)开始bfs,每次看看在两个自动机上走到的两个点儿子指针以及终态信息是否完全一致,是的话就把所有儿子指针都拉到队列中进行后面的判断。
由于我们对于每一个二元组,最多只需要判断一次,显然时间复杂度就不会超过n^2了。
不过我个人觉得题目好像有些问题,在题目给出的数据中,自动机可能存在一些“不合法”的状态,什么意思呢?就是说有的状态,在它的所有后继状态中没有任何一个可以到达终态,但是这种状态在构建自动机的时候应该不会被构造出来吧?题目此处让我很疑惑。
因此,我们需要从每一个终态开始沿着指针的反方向往回遍历一遍,看看那些状态是有用状态,然后bfs的时候只需要考虑有效的状态就好了。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define maxn 2020
#define pr pair<int,int>
#define mp(x,y) make_pair(x,y)
using namespace std; int next[][maxn][],tag[][maxn];
int n[],m;
bool vis[][maxn],walk[][maxn],f[maxn][maxn];
vector<int> from[][maxn]; void _init(int x)
{
for (int i=; i<n[x]; i++){
vis[x][i]=false;
walk[x][i]=false;
from[x][i].clear();
}
if (x==){
for (int i=; i<n[]; i++)
for (int j=; j<n[]; j++) f[i][j]=false;
}
} void visit(int x,int cur)
{
if (vis[x][cur]) return ;
vis[x][cur]=true;
for (unsigned i=; i<from[x][cur].size(); i++)
visit(x,from[x][cur][i]);
} bool match(int u,int v)
{
if (tag[][u]!=tag[][v]) return false;
for (int i=; i<m; i++)
if ((next[][u][i]>= && vis[][next[][u][i]])^(next[][v][i]>= && vis[][next[][v][i]])) return false;
return true;
} bool check()
{
queue<pr> Q;
Q.push(mp(,));
while (!Q.empty()){
int u=Q.front().first,v=Q.front().second;
Q.pop();
f[u][v]=true;
if (!match(u,v)) return false;
for (int i=; i<m; i++){
if (next[][u][i]> && vis[][next[][u][i]]){
if (f[next[][u][i]][next[][v][i]]) continue;
Q.push(mp(next[][u][i],next[][v][i]));
}
}
}
return true;
} int main()
{
int cas=;
while (scanf("%d",&m) && m){
for (int i=; i<; i++){
scanf("%d",&n[i]);
_init(i);
for (int j=; j<n[i]; j++){
scanf("%d",&tag[i][j]);
for (int k=; k<m; k++){
scanf("%d",&next[i][j][k]);
if (next[i][j][k]>=) from[i][next[i][j][k]].push_back(j);
}
}
for (int j=; j<n[i]; j++)
if (tag[i][j] && !vis[i][j]) visit(i,j);
}
printf("Case #%d: ",++cas);
if (check()) puts("Yes");
else puts("No");
}
return ;
}
HDU2471_History of Languages的更多相关文章
- PLoP(Pattern Languages of Programs,程序设计的模式语言)
2014/8/1 12:24:21潘加宇 http://www.umlchina.com/News/Content/340.htmPloP大会2014即将举行 PLoP(Pattern Languag ...
- Natural language style method declaration and usages in programming languages
More descriptive way to declare and use a method in programming languages At present, in most progra ...
- ECSHOP \admin\edit_languages.php GETSHELL Based On Injection PHP Code Into /languages/zh_cn/user.php
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 对于很多CMS网站来说,它们都需要保存很多的网站META信息,最常用的最佳实践是以 ...
- UVALive 6523 Languages
传送门 The Enterprise has encountered a planet that at one point had been inhabited. The only remnant f ...
- Scripting Languages
Computer Science An Overview _J. Glenn Brookshear _11th Edition A subset of the imperative programmi ...
- 1.6.7 Detecting Languages During Indexing
1. Detecting Languages During Indexing 在索引的时候,solr可以使用langid UpdateRequestProcessor来识别语言,然后映射文本到特定语言 ...
- The future of programming languages
In this video from JAOO Aarhus 2008 Anders Hejlsberg takes a look at the future of programming langu ...
- Languages
Languages A language class exists inside the system/Core folder, this class have 2 methods: load - L ...
- BZOJ3296: [USACO2011 Open] Learning Languages
3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
随机推荐
- scala-类
---恢复内容开始--- 随笔记录scala中,有哪些类,如何定义一个类,有哪些注意点. 一,scala中有哪些类? 1,简单类 class 2,单例模式 object 3,伴生类 4,case cl ...
- sublime 关闭自动更新
第一步: 点击菜单栏“Preferences”=> "Settings-User" 进入个人参数设置页面: 第二步: 在大括号内插入如下代码:"update_che ...
- RequireJS基础(二)
上一篇是把整个jQuery库作为一个模块.这篇来写一个自己的模块:选择器. 为演示方便这里仅实现常用的三种选择器id,className,attribute. RequireJS使用define来定义 ...
- mysql 允许远程访问
原来装mysql数据库的时候就想,这个只要在本地访问就可以了,没有必要让远程访问.可是,今天想把数据放到远程的机器上,却发现,不是简单的将sql语句(指导出的语句)执行一遍就行了,对于那些自增的字段, ...
- RunLoop(官方文档翻译)
循环运行 运行循环是与线程相关联的基本基础设施的一部分.一个运行循环是用于调度工作,并协调接收传入事件的事件处理循环.一个运行循环的目的是让你的线程繁忙时,有工作要做,把你的线程时有没有睡觉. 循环运 ...
- arguments 对象
在函数体内,标识符arguments是指向实参对象的引用,实参对象是一个类数组对象 arguments[0],arguments.length arguments是什么? 答:1:arguments是 ...
- jQuery小节
jQuery 语法 jQuery 选择器 在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素. jQuery 元 ...
- json_decode返回null 和synax error原因及处理
$checkLogin ='[{"gdsincode":"1103293","gdsname":"鲜美来带鱼段800g" ...
- linux命令(3):pwd命令
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...
- js常用
window.navigator.userAgent.toLowerCase().indexOf("msie") != -1 是否是IE浏览器