题意:输入四个字符串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; } }