为什么用不了swap啊

P1032 [NOIP2002 提高组] 字串变换

希望更丰富的展现?使用Markdown 楼主的代码: ```cpp #include<iostream> #include<cstdio> #include<functional> #include<cstring> #include<string> #include<queue> #include<algorithm> using namespace std; string s1[6],s2[6]; int n = 1;//n个规则 int bfs(string a, string b) { queue<string> q; queue<string> q1; int step = 0; q.push(a); while (step!=10) { int flag = 1; while (!q.empty()) { string st = q.front(); q.pop(); if (st == b) { cout << step; return 0; } for (int i = 1; i <= n; i++) { string str = st; int bpos = 0; while (str.find(s1[i], bpos) != -1) { int post = str.find(s1[i], bpos); bpos = post + 1; flag = 0; str.erase(post, s1[i].length()); str.insert(post, s2[i]); q1.push(str); str = st; } } } if (flag) { cout << "NO ANSWER!"; return 0; } step++; q.swap(q1); } } int main() { string a, b; cin >> a >> b; while (cin >> s1[n] >> s2[n]) n++; n--; bfs(a,b); return 0; } ```
by agicy @ 2018-08-14 16:47:51


```cpp #include<iostream> #include<cstdio> #include<functional> #include<cstring> #include<string> #include<queue> #include<algorithm> using namespace std; string s1[6],s2[6]; int n = 1;//n个规则 int bfs(string a, string b) { queue<string> q; queue<string> q1; int step = 0; q.push(a); while (step!=10) { int flag = 1; while (!q.empty()) { string st = q.front(); q.pop(); if (st == b) { cout << step; return 0; } for (int i = 1;i <= n;i++) { string str = st; int bpos = 0; while (str.find(s1[i], bpos) != -1) { int post = str.find(s1[i], bpos); bpos = post + 1; flag = 0; str.erase(post, s1[i].length()); str.insert(post, s2[i]); q1.push(str); str = st; } } } if (flag) { cout << "NO ANSWER!"; return 0; } step++; q.swap(q1); } } int main() { string a, b; cin >> a >> b; while (cin >> s1[n] >> s2[n]) n++; n--; bfs(a,b); return 0; } ```
by 可乐配代码 @ 2018-08-14 16:48:17


@[可乐配代码](/space/show?uid=98040) 好像把`q.swap(q1);`改成`swap(q,q1);`就可以了。
by agicy @ 2018-08-14 16:50:19


|