POJ-1373

L-system
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 841 Accepted: 77

Description

A D0L (Deterministic Lindenmayer system without interaction) system consists of a finite set SIGMA of symbols (the alphabet), a finite set P of productions and a starting string w. The productions in P are of the form x -> u, where x in SIGMA and u in SIGMA+ (u is called the right side of the production), SIGMA+ is the set of all strings of symbols from SIGMA excluding the empty string. Such productions represent the transformation of the symbol x into the string u. For each symbol x in SIGMA , P contains exactly one production of the form x -> u. Direct derivation from string u1 to u2 consists of replacing each occurrence of the symbol x in SIGMA in u1 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 w 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 -> u, b -> v, where u and v in {a,b}+, and the starting string w in {a,b}+. 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 SIGMA*, SIGMA* is the set of all strings of symbols from SIGMA, including the empty string.). Certainly you can. Write the program which will solve this problem. 

Input

The input 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 there is one line in the output 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

Source


#include <stdio.h>
#include <string.h>

#define CHARMAX   16
#define QUEUEMAX  10000

char u[CHARMAX], v[CHARMAX], w[CHARMAX], z[CHARMAX];

int hash_inset(char *s, int *h)
{
     int sum;
     for (sum = 0; *s; s++) {
         sum = sum*2 + *s -'a' + 1;
     }
     if (h[sum])
        return 0;
     h[sum] = 1;
     return 1;
}

void change(char *f, char *t)
{
     *f = '\0';
     for (; *t; t++) {
         if (*t == 'a')
            strcat(f, u);
         if (*t == 'b')
            strcat(f, v);
     }
}

int bfs()
{
     int front, rear, i;
     int hash[70000] = {0};
     char queue[QUEUEMAX][CHARMAX] = {0}, str[CHARMAX*15] = {0}, son[CHARMAX] = {0};
     char *open;
     
     if (strstr(w, z)){
        return 1;
     }
     
     front = rear = 0;     
     strcpy(queue[rear++], w);
     hash_inset(queue[front], hash);
     while (front != rear) {
           change(str, queue[front]);
           ++front;
           front %= QUEUEMAX;
           if (strstr(str, z)){
              return 1;
           }
           for (i = strlen(z), open = str; *open; open++) {
               strncpy(son, open, i);
               if (hash_inset(son, hash)) {
                  strcpy(queue[rear++], son);
                  rear %= QUEUEMAX;
               }
           }
     }
     return 0;
}

int main()
{
    while (gets(u) != NULL) {
          gets(v);
          gets(w);
          gets(z);
          if (!z[0])
             printf("YES\n");
          else if (bfs())
                  printf("YES\n");
          else
              printf("NO\n");
          
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值