uva-310-L--system-暴力枚举

  题意:输入四个字符串a,b,w,z,经过一定的替换规则,问w或者w的子串中是否包含z.

替换规则如下.w中的字符a全部替换成a字符串,b字符全部替换成b字符串.

枚举过程,

根据替换规则对w进行替换,生成新的字符串w2,

对w2的子串中长度小于等于z的字符串全部枚举一遍,题目输入限制n<=16,那么,w的子串最多为2^16

#include <iostream>
#include<map>
#include<memory.h>
#include<stdio.h>
#include<string>
#include<queue>
#include<vector>
using namespace std;
string a;
string b;
string bg;
string ed;
map<string, int>maps;
queue<string>q;
int ok = 0;


void judge(string temp)
{
    //枚举字串
    for (int i = 0;i < temp.size() - 1;i++)
    {
        string temp2 = "";
        for (int j = i;j < i + ed.size() && j < temp.size();j++)
        {
            temp2 += temp[j];
        }
        if (temp2 == ed)
        {
            ok = 1;
            return;
        }
        else if (maps[temp2] == 0)
        {
            q.push(temp2);
            maps[temp2] = 1;
        }
    }
}

void bfs(string temp)
{

    if (temp.size() >= ed.size())
        judge(temp);
    if (ok)
        return;
    q.push(temp);
    maps[temp] = 1;
    while (q.empty() == false)
    {
        temp = q.front();
        q.pop();
        string temp2 = "";
        for (int i = 0;i < temp.size();i++)
        {
            if (temp[i] == 'a')
                temp2 += a;
            if (temp[i] == 'b')
                temp2 += b;
        }
        judge(temp2);
        if (ok)
            return;
    }
}

int main()
{
    while (cin >> a)
    {
        cin >> b >> bg >> ed;
        ok = 0;
        maps.clear();
        while (q.empty() == false)
            q.pop();
        bfs(bg);
        if (ok)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;

    }
}

 

posted on 2018-10-28 15:49 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/9865836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值