每日一题 day19 打卡
Analysis
广搜+map判重
用find寻找字串,再用replace替换字串
这里的map相当于正常广搜的一个book的作用
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 }
请各位大佬斧正(反正我不认识斧正是什么意思)