zoukankan      html  css  js  c++  java
  • 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;
    
        }
    }
  • 相关阅读:
    Autoit对win系统弹窗的操作
    Linux服务器测试网络连通性
    如何给linux配置两个不同网段的ip
    记下看过并觉得非常有用的文章
    使用python+selenium对12306车票数据读取
    windows系统mysql安装
    Python使用正则匹配re实现eval计算器
    css3[补1]
    Javascript[2]
    Javascript[1]
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/9865836.html
Copyright © 2011-2022 走看看