用的DFS,#5WA,显示找不到

P1032 [NOIP2002 提高组] 字串变换

```cpp #include <iostream> #include <vector> #include <queue> using namespace std; string a, b; vector<string> before; vector<string> after; bool found = false; int min_d = 120999,k; void dfs(string cur_str, int depth) { //if (cur_str[0] != b[0]) return; //cerr << cur_str << " " << depth << endl; if (cur_str == b) { if (depth < min_d) min_d = depth; found = true; return; } if (depth == k) { return; } for (int i = 0; i < before.size(); ++i) { string::size_type f = cur_str.find(before[i]); while (f != string::npos) { string temp = cur_str; temp.replace(f, before[i].size(), after[i]); dfs(temp, depth+1); f = cur_str.find(before[i],f + 1); } } } int main() { cin >> a >> b; string temp_a, temp_b; while (cin >> temp_a >> temp_b) { before.push_back(temp_a); after.push_back(temp_b); } for (k = 1;k <= 10;k++){ dfs(a, 0); if (found){ cout<<k; return 0; } } if (!found) { cout << "NO ANSWER!" << endl; return 0; } cout << min_d << endl; } ``` @[Jasonde1024](/user/605777)
by 某个新手 @ 2022-08-01 12:51:17


|