zoukankan      html  css  js  c++  java
  • uva 310 L--system(隐式图搜索+字符串处理)

     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 , btex2html_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

    题目大意:给出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] = '';
    	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] = '';
    	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] = '';
    	    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
    ", bfs() ? "YES" : "NO");
        }
        return 0;
    }
  • 相关阅读:
    Spring Boot初学
    Spring MVC必须知道的执行流程
    日志Log4j使用
    SpringMVC处理中文乱码
    Maven设置阿里云镜像
    Maven项目中配置文件导出问题
    使用Limit实现分页
    web项目中设置首页
    JVM 学习笔记记录
    Python内存管理&垃圾回收机制
  • 原文地址:https://www.cnblogs.com/pangblog/p/3266531.html
Copyright © 2011-2022 走看看