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时,倒水的最小体积是多 ...
随机推荐
- [RxJS] Completing a Stream with TakeWhile
Subscribe can take three params: subscribe( (x)=> console.log(x), err=> console.log(err), ()=& ...
- Android Fragment详解(四):管理Fragment
要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager(). 你可以用FragmentManager来做以上事情: ...
- js点击打开弹窗
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 超长英文(代码)自动换行的样式(CSS)
如何想让一连串文字在显示可以自动换行,而不会把在代码中使用的容器撑开,则在文章的CSS样式处加上以下代码即可: table-layout: fixed; word-wrap:break-word;或者 ...
- oracle包详解(二)【weber出品】
一.重载子程序 PL/SQL中的重载功能: 1. 能够使用户创建两个或多个同名的子程序 2. 要求子程序的形式参数的数量,顺序或者数据类型不一样 3. 能够使用户使用不同的数据灵活的调用子程序 4. ...
- .net 将xml转换成DateSet
/// <summary> /// 将XML字符串转换成DATASET /// </summary> /// <param name="xmlStr" ...
- JavaScript的数据类型转换
首先,由于JavaScript是弱类型语言(弱类型的语言的东西没有明显的类型,他能随着环境的不同,自动变换类型而强类型则没这样的规定,不同类型间的操作有严格定义,只有相同类型的变量才能操作,虽然系统也 ...
- c#中serialPort1_DataReceived串口接收事件处理
1.缓冲区不定字节读取(波特率很高也没问题) //Thread.sleep(1000);//处理事件这块可以加上延时确保不定数的数据可以全部收到缓冲后,才去读缓冲内容--单位:毫秒 byte[] da ...
- 获取当前PHP运行环境是否cli模式
需要用到系统函数php_sapi_name() 或者 系统常量 PHP_SAPI,返回 cli 或 cli_server /* 判断当前的运行环境是否是cli模式 */ function is_cli ...
- CSU 1120 病毒
最长公共上升子序列(LCIS) 裸的算法题. 动态规划: 两组数组a[n].b[m]. f[i][i]表示以a[i].b[j]结尾的两个数组的LCIS. 转移方程: a[i]!=b[j] : f[i] ...