Luo Gu P1032 string conversion solution to a problem

Daily questions day19 punch

Analysis

+ Map sentenced to heavy wide search

Looking for a string find, and then replace the replacement string

map here is equivalent to the role of a book of the normal wide search

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<map> 
 7 #define int long long
 8 #define maxn 6+10
 9 using namespace std;
10 inline void write(int x)
11 {
12     if(x<0){putchar('-');x=-x;}
13     if(x>9)write(x/10);
14     putchar(x%10+'0');
15 }
16 string a,b;
17 string ca[maxn],cb[maxn];
18 map<string,int> m;
19 int len=1;
20 queue<string> cq;
21 queue<int> sq;
22 inline int bfs()
23 {
24     while(!cq.empty()&&cq.front()!=b&&sq.front()<=10)
25     {
26         if(m[cq.front()]==1)
27         {
28             cq.pop();
29             sq.pop();
30             continue;
31         }
32         m[cq.front()]=1;
33         for(int i=1;i<=len;i++)
34         {
35             string ns=cq.front();
36             while(1)
37             {
38                 int site=ns.find(ca[i]);
39                 if(site==-1) break;
40                 string ss=cq.front();
41                 ss.replace(site,ca[i].size(),cb[i]);
42                 cq.push(ss);
43                 sq.push(sq.front()+1);
44                 ns[site]='#';
45             }
46         }
47     }
48     if(cq.empty()||sq.front()>10) return -1;
49     else return sq.front();
50 }
51 signed main()
52 {
53     cin>>a>>b;
54     while(cin>>ca[len]>>cb[len]) len++;
55     len--;
56     if(len==0&&a!=b)
57     {
58         cout<<"NO ANSWER!";
59         return 0;
60     }
61     cq.push(a);
62     sq.push(0);
63     int ans=bfs();
64     if(ans==-1) cout<<"NO ANSWER!";
65     else write(ans);
66     return 0;
67 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11575458.html