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时,倒水的最小体积是多 ...
随机推荐
- hdu4055 dp
http://acm.hdu.edu.cn/showproblem.php?pid=4055 Problem Description The signature of a permutation is ...
- timed out waiting for input: auto-logout
The ssh "timed out waiting for input: auto-logout" messages is generated by ssh upon reach ...
- mongodb 详解 error:10061 由于目标计算机积极拒绝,无法连接解决方法
mongodb下载地址(32位):下载地址 自己选择版本 建立如下与mongodb并行的两个文件夹data和log. 然后建立mongo.config. 在mongo.config配置文件中输入: # ...
- asp.net中的主题
用了更方便,在.net网站上,右键,选择添加主题,然后命一个名子,如果,有四个主题, 只需要在web.config的page节里加上styleSheetTheme="Red",就会 ...
- oracle数据库连接
///宁采花 8:37:39 /// <summary> /// 获取数据链接 /// </summary> /// <returns></returns&g ...
- 如何禁用不需要的HTTP方法
IIS7.0默认开启了不安全的OPTIONS和TRACE方法,建议关闭这两个方法. 以下环境为windows server 2008.IIS7.0 方法(1):web.config 在<conf ...
- C++中的cout输出机制
代码: #include <iostream> using namespace std; int hello(){ cout<<"hello"<< ...
- 洛谷 P1241 括号序列
P1241 括号序列 题目描述 定义如下规则序列(字符串): 1.空序列是规则序列: 2.如果S是规则序列,那么(S)和[S]也是规则序列: 3.如果A和B都是规则序列,那么AB也是规则序列. 例如, ...
- 无法捕获的异常:MissingMethodException
今天一个同事发布站点,一直出现一些稀奇古怪的问题,各种各样的异常都有,根据这些异常去排查代码,都完全正常,很让人郁闷,因为代码里可能出异常的地方都记录了程序日志,所以他一直没去排查系统里的“应用程序日 ...
- Android开源框架之SwipeListView导入及模拟QQ侧滑
SwipeListView是Github上的一个开源框架,地址:https://github.com/47deg/android-swipelistview SwipeListView was bor ...