【题解】luogu p1032 字串变换
生活随笔
收集整理的這篇文章主要介紹了
【题解】luogu p1032 字串变换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
總結(jié):
1.bfs的模板沒(méi)有掌握好
2.string s 的時(shí)候可以用s[m]=x 將s字符串中的m位置的字符改變成x字符
?
一道簡(jiǎn)單的bfs
#include<bits/stdc++.h> using namespace std; string s1, s2; string a[10], b[10]; int cnt = 1, m; queue <string> q; queue <int> num; map <string, int> mapp;int bfs() {string s, ss;while(q.empty() == 0 && q.front() != s2 && num.front() <= 10){if(mapp[q.front()] == 1){q.pop();num.pop();continue;} mapp[q.front()] = 1;for(int i = 1; i <= cnt; i++){s = q.front();while(1){m = s.find(a[i]);if(m == -1) break;ss = q.front();ss.replace(m, a[i].size(), b[i]);q.push(ss);num.push(num.front()+1);s[m] = '.';}}q.pop();num.pop();} if(q.empty() == 1 || num.front() > 10) return -1;else return num.front(); }int main() {cin >> s1 >> s2;while(cin >> a[cnt] >> b[cnt]) cnt++;cnt--;q.push(s1);num.push(0);int ans = bfs();if(ans == -1){cout << "NO ANSWER!";return 0;}else cout << ans;return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/lovezxy520/p/11484480.html
總結(jié)
以上是生活随笔為你收集整理的【题解】luogu p1032 字串变换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: tp5中在where中使用in
- 下一篇: 网站被截持到赌博网