洛谷P1032 字串变换【bfs】

题目链接:https://www.luogu.org/problemnew/show/P1032

题意:

给定一个原字符串和目标字符串,以及几个字符串变换的规则。

问能否根据这几个规则在十步之内把原字符串变为目标字符串。

思路:

 bfs,队列维护字符串和所经过的步骤这样一个二元组而不是简单的字符串。

每一次取出一个字符串,用所给的规则进行变换得到新的字符串。

由于字符串中可能有多次匹配,所以用vector存每一次匹配的开始位置,这些都是独立的一次变换都要单独存入vector中。【这一点最开始没有考虑到,图方便用了.find()】

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<map>
  4 #include<set>
  5 #include<iostream>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<vector>
  9 #include<queue>
 10 
 11 using namespace std;
 12 
 13 string A, B;
 14 struct rule{
 15     string a, b;
 16 }rules[7];
 17 struct node{
 18     string s;
 19     int steps;
 20     node(){}
 21     node(string _s, int _steps){
 22         s = _s;
 23         steps = _steps;
 24     }
 25 };
 26 set<string>sset;
 27 
 28 int main()
 29 {
 30     cin>>A>>B;
 31     int n = 0;
 32     while(cin>>rules[n].a>>rules[n].b)n++;
 33     
 34     queue<node>que;
 35     que.push(node(A, 0));
 36     int steps = 0;
 37     bool flag = false;
 38     while(!que.empty()){
 39         node now = que.front();que.pop();
 40         //cout<<now.s<<" "<<now.steps<<endl;
 41         if(now.s == B){
 42             flag = true;
 43             steps = now.steps;
 44             break;
 45         }
 46         if(now.steps == 10)continue;
 47         for(int i = 0; i < n; i++){
 48             //cout<<i<<endl;
 49             vector<int>pos;
 50             //cout<<rules[i].a.length()<<endl;
 51             //cout<<now.s.length()<<endl;
 52             //cout<<now.s.length() - rules[i].a.length()<<endl;
 53             for(int j = 0; j < now.s.length(); j++){
 54                 bool f = true;
 55                 for(int k = 0; k < rules[i].a.length(); k++){
 56                     if(j + k >= now.s.length()){
 57                         f = false;
 58                         break;
 59                     }
 60                     if(now.s[j + k] != rules[i].a[k]){
 61                         f = false;
 62                         break;
 63                     }
 64                 }
 65                 if(f)pos.push_back(j);
 66             }
 67             
 68             //cout<<pos.size()<<endl;
 69             for(int x = 0; x < pos.size(); x++){
 70                 int p = pos[x];
 71                 char t[15000];
 72                 int j;
 73                 for(j = 0; j < p; j++){
 74                     t[j] = now.s[j];
 75                     //cout<<t[j];
 76                 }
 77                 for(int k = 0; k < rules[i].b.length(); k++, j++)
 78                 {
 79                     t[j] = rules[i].b[k];
 80                 }
 81                 for(int k = p + rules[i].a.length(); k < now.s.length(); k++, j++){
 82                     t[j] = now.s[k]; 
 83                 }
 84                 t[j] = '\0';
 85                 
 86                 if(sset.find(t) == sset.end()){
 87                     que.push(node(t, now.steps + 1));
 88                     sset.insert(t);
 89                 }
 90             }
 91         }
 92     }
 93     if(flag){
 94         printf("%d\n", steps);
 95     }
 96     else{
 97         printf("NO ANSWER!\n");
 98     }
 99     return 0;
100 }

---恢复内容结束---

猜你喜欢

转载自www.cnblogs.com/wyboooo/p/10351233.html
今日推荐