uva 310 L--system(隐式图搜索+字符串处理)
| L-system |
A D0L (Deterministic Lindenmayer system without interaction) system consists of a finite set
of symbols (the alphabet), a finite set P of productions and a starting string
. The productions in P are of the form
, where
and
(u is called the right side of the production),
is the set of all strings of symbols from
excluding the empty string. Such productions represent the transformation of the symbol x into the string u. For each symbol
, P contains exactly one production of the form
. Direct derivation from string
to
consists of replacing each occurrence of the symbol
in
by the string on the right side of the production for that symbol. The language of the D0L system consists of all strings which can be derived from the starting string
by a sequence of the direct derivations.
Suppose that the alphabet consists of two symbols a and b. So the set of productions includes two productions of the form a
, b
, where u and
, and the starting string
. Can you answer whether there exists a string in the language of the D0L system of the form xzy for a given string z? (x and y are some strings from
,
is the set of all strings of symbols from
, including the empty string.). Certainly you can. Write the program which will solve this problem.
Input
The input file of the program consists of several blocks of lines. Each block includes four lines. There are no empty lines between any successive two blocks. The first line of a block contains the right side of the production for the symbol a. The second one contains the right side of the production for the symbol b and the third one contains the starting string
and the fourth line the given string z. The right sides of the productions, the given string z and the starting string
are at most 15 characters long.
Output
For each block in the input file there is one line in the output file containing YES or NO according to the solution of the given problem.
Sample Input
aa
bb
ab
aaabb
a
b
ab
ba
Sample Output
YES
NO
题目大意:给出a.b,begin, over四个字符串,要求判断是否能有begin转变成为over,转变的过程是将begin中的字符'a'转变成为a串, 字符’b'转变成b串。
解题思路:bfs, 每次将当前字符串中的所有字符ab装换为字符串ab,然后将新的到的字符串分离子串,判断子串是否满足,不满足的话判断是否重复出现,
未重复出现则标记。
#include <stdio.h>
#include <string.h> const int N = 1 << 16;
const int M = 16; int vis[N], end;
char a[M], b[M], begin[M], over[M];
char que[N][M], pdn[200]; int hash(char str[]) {
int sum = 0, cnt = strlen(str);
for (int i = 0; i < cnt; i++)
sum = sum * 2 + str[i] - 'a' + 1;
return sum % N;
} void inInit() {
memset(que, 0, sizeof(que));
memset(vis, 0, sizeof(vis));
scanf("%s%s%s", b, begin, over);
end = hash(over);
} bool bfs() {
inInit();
int t, i, j, k;
int front = 0, rear = 0;
for(i = 0; begin[i]; i++) {
for(k = 0,j = i; begin[j] && over[k]; k++, j++)
que[rear][k] = begin[j];
que[rear][k] = '\0';
t = hash(que[rear]); if(t == end)
return 1; if(!vis[t]) {
vis[t]=1;
rear++;
}
} while(rear > front) {
for(i = 0,k = 0; que[front][i]; i++) {
if(que[front][i] == 'a')
for(j = 0; a[j]; j++)
pdn[k++] = a[j];
else
for(j = 0; b[j]; j++)
pdn[k++] = b[j];
}
pdn[k] = '\0';
for(i = 0; pdn[i]; i++) {
for(k = 0,j = i; pdn[j] && over[k]; j++, k++)
que[rear][k] = pdn[j];
que[rear][k] = '\0';
t = hash(que[rear]);
if(t == end)
return true;
if(!vis[t]) {
vis[t] = 1;
rear++;
}
}
front++;
}
return false;
} int main() {
while (scanf("%s", a) == 1) {
printf("%s\n", bfs() ? "YES" : "NO");
}
return 0;
}
uva 310 L--system(隐式图搜索+字符串处理)的更多相关文章
- UVa 658 - It's not a Bug, it's a Feature!(Dijkstra + 隐式图搜索)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)
题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...
- 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)
这道题用到了很多知识点, 是一道好题目. 第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算. 第二这里是隐式 ...
- [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索
[HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...
- UVA 658 状态压缩+隐式图+优先队列dijstla
不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...
- 洛谷 P2622 关灯问题II【状压DP;隐式图搜索】
题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...
- 状态转移的最短路 隐式图搜索 UVA 658
紫书365 题目大意:给你n个全都是bug的东西,然后每次可以修复,给你修复前后的状态,问最后如果能把bug全都修复,最少需要多少时间. 思路:从最初状态开始,然后枚举bug即可. 表示priorit ...
- UVA - 10603 Fill(隐式图搜索)
题目大意:经典的倒水问题. 给你三个瓶子,体积为a,b,c. 刚開始a.b是空的,c是满的,如今要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多 ...
随机推荐
- 未知宽高div水平垂直居中3种方法
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head&g ...
- 使用apktool解包和打包apk
使用apktool解包和打包apk 下载apktool工具 解包 apktool d xxx.apk -f 植入代码 使用apktool解包要植入代码的apk(下面称为A), 使用apktool解包包 ...
- OC教程10-NSNumber具体
NSNumber简单介绍 NSNumber是数字的对象形式,由于在OC的数组和字典中仅仅同意存放对象,所以我们有时候须要转化 我们普通的类型是 123 那么 NSNumber类型的是 @123, ...
- C#获取时间戳的方法
获取时间戳的方法 /// <summary> /// 获取时间戳 /// </summary> /// <param name= ...
- 使用Dataset
string sqlStr="Select * from Tb_news"; SqlDataAdapter myDa=new SqlDataAdapter(SqlStr,myCon ...
- AngularJs练习Demo5
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- c# 中的线程和同步
一.新建线程的3种方法 a)异步委托:b)Thread类:c)线程池: 二.异步委托 1.简单使用,检查委托是否完成其任务 a) 通过 BeginInvoke() 的返回值IAsyncResult ...
- [Database] Deadlock avoidance protocol
如何避免Deadlock,如果我们能提前知道各个Process对于资源的需求情况,我们就可以用Banker's algorithm (银行家算法) 来解决问题.可是这在现在中不好实现,因为很难提前知道 ...
- apache将请求转发到到tomcat应用
映射: 1.开启apache中的proxy模块(proxy.conf,proxy.load,proxy_http.load) 2.配置apache配置文件,<VirtualHost *:80&g ...
- java生成UUID通用唯一识别码 (Universally Unique Identifier)
转自:http://blog.csdn.net/carefree31441/article/details/3998553 UUID含义是通用唯一识别码 (Universally Unique Ide ...