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(隐式图搜索+字符串处理)的更多相关文章

  1. 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& ...

  2. uva 10274 Fans and Gems(隐式图搜索+模拟)

    Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...

  3. 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)

    题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...

  4. 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)

    这道题用到了很多知识点, 是一道好题目.      第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算.     第二这里是隐式 ...

  5. [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索

    [HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...

  6. UVA 658 状态压缩+隐式图+优先队列dijstla

    不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...

  7. 洛谷 P2622 关灯问题II【状压DP;隐式图搜索】

    题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...

  8. 状态转移的最短路 隐式图搜索 UVA 658

    紫书365 题目大意:给你n个全都是bug的东西,然后每次可以修复,给你修复前后的状态,问最后如果能把bug全都修复,最少需要多少时间. 思路:从最初状态开始,然后枚举bug即可. 表示priorit ...

  9. UVA - 10603 Fill(隐式图搜索)

    题目大意:经典的倒水问题. 给你三个瓶子,体积为a,b,c. 刚開始a.b是空的,c是满的,如今要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多 ...

随机推荐

  1. andorid 开发笔记 -- 问题与解决

    1. SQLiteDataBase 中 TimeStamp 转化为 Date 的问题:java.text.ParseException: Unparseable date: "Sun Jan ...

  2. JavaScript 字符串函数 之查找字符方法(一)

    1.JavaScript查找字符方法   首先看一张 有关字符串函数的查找字符的方法图 接下里补充里面的方法说明 2.charAt()     charAt() 方法返回字符串中指定位置的字符 语法 ...

  3. javascript将毫秒还原为可读时间格式

    <script type="text/javascript"> //随便设置一个时间 var otime = new Date("2015-11-11 20: ...

  4. 正则去掉html标签

    return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "&qu ...

  5. Visual Studio在页面按F7不能跳转至cs代码页的解决方法

    检查页面Page设置内的CodeBehind属性,看是否与代码页的文件名相同,不同则改正,问题得以解决.

  6. C#图像处理(5):无损保存图片

    C#使用默认方法对图像进行保存的时候图像会有损失,以下提供无损保存的方法: /// <summary> /// 无损保存图片 /// </summary> /// <pa ...

  7. Cocos2d-x 3.0 场景切换

    场景切换要用到导演类Director,一般有两种方式,大多数是用替换场景(replaceScene),也可以用进栈(pushScene)出栈(popScene)的方式进行场景的替换. 场景切换代码: ...

  8. JSON基础知识

    1.什么是json •        JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) •        JSON 是轻量级的文本数据交换格式 ...

  9. Python正则匹配字母大小写不敏感在读xml中的应用

    需要解决的问题:要匹配字符串,字符串中字母的大小写不确定,如何匹配? 问题出现之前是使用字符串比较的方式,比如要匹配'abc',则用语句: if s == 'abc':#s为需要匹配的字符串 prin ...

  10. Nlog从下载到使用例子

    第一.首先下载nlog.dll 下载地址:http://pan.baidu.com/s/1i3DQsV7 第二.添加nlog.ll的引用 第三.代码 private static Logger log ...