uva 310 L--system

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=246

题目描述:

L-system

A D0L (Deterministic Lindenmayer system without interaction) system consists of a finite set tex2html_wrap_inline36 of symbols (the alphabet), a finite set P of productions and a starting string tex2html_wrap_inline40 . The productions in P are of the form tex2html_wrap_inline44 , where tex2html_wrap_inline46 and tex2html_wrap_inline48 (u is called the right side of the production), tex2html_wrap_inline52 is the set of all strings of symbols from tex2html_wrap_inline36 excluding the empty string. Such productions represent the transformation of the symbol x into the string u. For each symbol tex2html_wrap_inline46 , P contains exactly one production of the form tex2html_wrap_inline44 . Direct derivation from string tex2html_wrap_inline66 to tex2html_wrap_inline68 consists of replacing each occurrence of the symbol tex2html_wrap_inline46 in tex2html_wrap_inline66 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 tex2html_wrap_inline40 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 tex2html_wrap_inline76 , b tex2html_wrap_inline78 , where u and tex2html_wrap_inline82 , and the starting string tex2html_wrap_inline84 . 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 tex2html_wrap_inline94 , tex2html_wrap_inline94 is the set of all strings of symbols from tex2html_wrap_inline36 , 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 tex2html_wrap_inline40 and the fourth line the given string z. The right sides of the productions, the given string z and the starting string tex2html_wrap_inline40 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

题意:

w串是否能到达一个包含z串的xzy串,变化过程是a替换成a串 b替换成b串

题解:

实在没懂这个L system,以及替换的策略,最后看了网上的题解,才对问题的大体有了了解,结果思路也是看到了,所以写出来的代码思路基本和题解的一样。
BFS + MAP Hash,
这里BFS的策略是变换得到一个串,然后这个串的还在是同等z串长度的子串,然后对这些子串进行扩展,并记录hash,直到找到匹配的z串或者所有的子串都已经扩展过了,YES or NO的答案也就出来了。
一个串如果包含z串那么它的任意子串也必定能扩展出包含z串的串。

代码:

代码1(stl queue)
#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
#include<queue>
using namespace std;
map<string, bool> vis_map;
string ar,br,w,z;
queue<string> queue_str;
int try_to_insert_sub(string elem)
{
    string s = "";
    int len_e = elem.length();
    for(int i = 0; i <= len_e - 1; i++)
    {
        if(elem[i] == 'a') s += ar;
        else s += br;
    }
    int len_s = s.length();
    int len_z = z.length();
    if(len_s <= len_z)
    {
        if(s == z) return 1;
        else
            if(!vis_map[s])
            {
                queue_str.push(s);
                vis_map[s] = true;
            }
    }
    else
    {
        for(int i = 0; i <= len_s - len_z; i++)
        {
            string sub_str = s.substr(i, len_z);
            if(sub_str == z) return 1;
            else
                if(!vis_map[sub_str])
                {
                    queue_str.push(sub_str);
                    vis_map[sub_str] = true;
                }
        }
    }
    return 0;
}
int bfs()
{
    vis_map.clear();
    while(!queue_str.empty()) queue_str.pop();
    queue_str.push(w);
    vis_map[w] = true;
    int flag = 0;
    while(!queue_str.empty())
    {
        string elem = queue_str.front();
        queue_str.pop();
        flag = try_to_insert_sub(elem);
        if(flag) break;
    }
    if(flag) printf("YES\n");
    else printf("NO\n");
    return(0);
}
int main()
{
    while((cin>>ar) != NULL)
    {
        cin>>br;
        cin>>w;
        cin>>z;
        if(w.find(z, 0) != string::npos) printf("YES\n");
        else bfs();
    }
    return(0);
}
代码2(数组 queue)
#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
#include<queue>
using namespace std;
map<string, bool> vis_map;
string ar,br,w,z;
string sq[400000];
int fq, rq;//front and rear
int try_to_insert_sub(string elem)
{
    string s = "";
    int len_e = elem.length();
    for(int i = 0; i <= len_e - 1; i++)
    {
        if(elem[i] == 'a') s += ar;
        else s += br;
    }
    int len_s = s.length();
    int len_z = z.length();
    if(len_s <= len_z)
    {
        if(s == z) return 1;
        else
        {
            if(!vis_map[s])
            {
                sq[rq++] = s;
                vis_map[s] = true;
            }
        }
    }
    else
    {
        for(int i = 0; i <= len_s - len_z; i++)
        {
            string sub_str = s.substr(i, len_z);
            if(sub_str == z) return 1;
            else
            {
                if(!vis_map[sub_str])
                {
                    sq[rq++] = sub_str;
                    vis_map[sub_str] = true;
                }
            }
        }
    }
    return 0;
}
int bfs()
{
    vis_map.clear();
    fq = rq = 0;
    sq[rq++] = w;
    vis_map[w] = true;
    int flag = 0;
    while(fq < rq)
    {
        string elem = sq[fq++];
        flag = try_to_insert_sub(elem);
        if(flag) break;
    }
    if(flag) printf("YES\n");
    else printf("NO\n");
    return(0);
}
int main()
{
    while((cin>>ar) != NULL)
    {
        cin>>br;
        cin>>w;
        cin>>z;
        if(w.find(z, 0) != string::npos) printf("YES\n");
        else bfs();
    }
    return(0);
}

你可能感兴趣的:(hash,bfs,L--system)