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;
}