1 条题解
-
0
题意: w串是否能到达一个包含z串的xzy串,变化过程是a替换成a串 b替换成b串
题解: 实在没懂这个L system,以及替换的策略,最后看了网上的题解,才对问题的大体有了了解,结果思路也是看到了,所以写出来的代码思路基本和题解的一样。 BFS + MAP Hash, 这里BFS的策略是变换得到一个串,然后这个串的还在是同等z串长度的子串,然后对这些子串进行扩展,并记录hash,直到找到匹配的z串或者所有的子串都已经扩展过了,YES or NO的答案也就出来了。 一个串如果包含z串那么它的任意子串也必定能扩展出包含z串的串。
#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 && !cin.eof()) { cin>>br; cin>>w; cin>>z; if(w.find(z, 0) != string::npos) printf("YES\n"); else bfs(); } return(0); }
- 1
信息
- ID
- 374
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者