UVA - 11604 General Sultan 题解
题目大意:
有若干模式串,将某些模式串拼接起来(一个可以使用多次)形成一个长模式串,判断能否有两种或更多种不同的拼法拼成相同的模式串。
思路:
神奇的构图,暴力的求解。
可以发现,若有不同的拼法,则一个模式串的前缀要与一个模式串的后缀相同。
因此我们就将问题转化成:从两个模式串开始,不停的按照前后缀匹配,最后达到两个串同时在一个点结束。
那么,将每一个串的每一个字符都看作一个点,n2len2暴力枚举i串从z开始的后缀和j串(自己也可以,但不能让前缀是其本身)的前缀做匹配,看是否能将其中一个串匹配完。
当(i,z)和j匹配,若i先结束了,则将(i,z)这个点连边到j串没匹配的第一个点,表示下一次匹配应该是从j的那个位置开始当后缀再寻找其他前缀;如果j先结束,与之前一个类似;如果i和j同时结束,则连边到终点。
最后用dfs判断能否从某个模式串的第一个点开始走到终点。
PS:1、fromhttp://blog.csdn.net/houserabbit/article/details/38943645
2、数组的意义、范围要清楚,我花了一个多小时才发现数组开小了。
代码:
#include<cstdio>
#include<cstring>
using namespace std;
const int M=,N=;
int n,e,t,i,j,k,z,cnt,v[M*M*N*N],id[M][N],nex[M*M*N*N],len[M],head[M*N];
bool vis[M*N],flag;
char s[M][N]; void add(int x,int y) { v[++cnt]=y,nex[cnt]=head[x],head[x]=cnt; } void dfs(int x)
{
if (x==e) { flag=; return; }
vis[x]=;
for (int i=head[x];~i;i=nex[i])
if (!vis[v[i]])
{
dfs(v[i]);
if (flag) return;
}
} int main()
{
while (~scanf("%d",&n))
{
if (!n) break;
e=cnt=flag=;
for (i=;i<=n;++i)
{
scanf("%s%s",s[i],s[i]);
len[i]=strlen(s[i]);
for (j=;j<len[i];++j) id[i][j]=++e;
}
for (++e,i=;i<=e;++i) head[i]=-;
for (i=;i<=n;++i)
for (z=;z<len[i];++z)
for (j=;j<=n;++j)
{
if (i==j && !z) continue;
for (k=;k<len[j] && z+k<len[i];++k)
if (s[i][z+k]!=s[j][k]) break;
if (z+k==len[i] && k==len[j]) add(id[i][z],e);
else if (k==len[j]) add(id[i][z],id[i][z+k]);
else if (z+k==len[i]) add(id[i][z],id[j][k]);
}
for (i=;i<=e;++i) vis[i]=;
for (i=;i<=n;++i)
{
if (!vis[id[i][]]) dfs(id[i][]);
if (flag) break;
}
printf("Case #%d: ",++t);
if (flag) puts("Ambiguous."); else puts("Not ambiguous.");
}
return ;
}
UVA - 11604 General Sultan 题解的更多相关文章
- General Sultan UVA - 11604(建图暴力)
给出n个字符串,询问是否存在一个字符串(可以是给出的几个中的 也可以是组合成的),使得用字符串(随便你用多少个)来拼凑这个串,能够至少有两种拼法 解析: 把每一个字符串的每一个位置的字符看作结点,进行 ...
- UVA 167 R-The Sultan's Successors
https://vjudge.net/contest/68264#problem/R The Sultan of Nubia has no children, so she has decided t ...
- Uva 167 The Sultan's Successors(dfs)
题目链接:Uva 167 思路分析:八皇后问题,采用回溯法解决问题. 代码如下: #include <iostream> #include <string.h> using n ...
- uva 167 - The Sultan's Successors(典型的八皇后问题)
这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include < ...
- 【Uva 11604 编码都有歧义了】
·你的目的就是要让编码有歧义,这就美妙了. ·英文题,述大意: 给出n个模板字符串,询问是否存在一个字符串,使得用模板串(随便你用多少个)来拼凑这个串,能够至少有两种拼法.如果有,就输出“ ...
- UVa 202 Repeating Decimals 题解
The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle ...
- UVA 10924 Prime Words 题解
Prime Words A prime number is a number that has only two divisors: itself and the number one. Exampl ...
- UVA 10852 Less Prime 题解
Less Prime Let n be an integer, 100 n 10000, nd the prime number x, x n, so that n
- UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...
随机推荐
- jqueryUI弹出框问题
jqueryui dialog中 select选不中或比较慢 dialog = function(Window,dialogDivId,title,buttons,css) { css = css|| ...
- MyBatis处理一行数据-MyBatis使用sum语句报错-MyBatis字段映射-遁地龙卷风
第二版 (-1)写在前面 我用的是MyBatis 3.2.4 (0) 编程轶事 select sum(value) ,sum(value2) from integral_list where Me ...
- MAC下apache+php
mac下是自带有Apache和php的服务器的,不需要另外安装,本文就对相关配置进行介绍. 第一:Apache 在终端中输入,下面指令即可启动Apache服务器: //启动 sudo apachect ...
- JAVA基础学习——1.2 环境搭建 之eclipse安装及中文化
安装好jdk,配置好环境变量以后,下面就可以进行安装eclipse了. 闲话少说,eclipse下载地址:http://www.eclipse.org/downloads/ 不大用关注checksum ...
- How to create a Python dictionary with double quotes as default quote format?
couples = [ ['jack', 'ilena'], ['arun', 'maya'], ['hari', 'aradhana'], ['bill', 'samantha']] pairs = ...
- CSS垂直居中
一.单行文本垂直居中: 设置其文本的行高line-height与其父容器高度相等即可.如 <style> .test{ background-color: grey; line-heigh ...
- SVN安装及使用
1.SVN的安装 1.1 安装软件 以下图1中两个图标,分别是SVN服务器端和客户端安装软件 图1 1.2 服务器安装步骤 先点击图1第一个图标,安装SVN的服务器,默认安装即可,安装成功的界面如下图 ...
- WPF中运行时使内容可以上下左右被鼠标拖动应该怎么做?
<Window x:Class="testGridSplitter.MainWindow" xmlns="http://schemas.microsoft.com/ ...
- React学习笔记-5-初始化阶段介绍
初始化阶段可以使用的函数:getDefaultProps:只调用一次,实例之间共享引用.只有在组件的第一个实例被初始化的时候,才会调用他,然后react会把这个函数的返回结果保存起来,从第二个实例开始 ...
- git bash操作
1. GIT说明 1> git是分布式,或者说是去中心化的.表现为: 开发者的可以在本地使用git并完美的控制自己的版本,而无需与服务端交互: 开发者可以将本地库在某个服务端备份,这种情况类似S ...