Typical String Conversion problem: http://codevs.cn/problem/1099/
I just learned breadth search yesterday and used it today. At the beginning, Baidu saw that everyone was using Bidirectional breadth search. Now I still don't understand the principles of Bidirectional search, you don't need to judge it !!! It's easy to understand that it's fast. Okay, cool! Today's CCF certification test, and tomorrow's two-way study!
First, let's talk about this. The most important thing about the wide search is the two points:
1. How to Create a search tree?
2. How do I determine duplicate statuses?
First, how can the status of each node be extended? In this case, it is of course the string search. Every time you find a position that can be replaced, it is an extensible state. In the first place, I searched for it only once, and then wa looked at it and fixed my eyes. It turns out that multiple strings can be replaced, these replaceable locations are also the status that needs to be extended, so a while loop is added, and everything is done !!!
Second, how can we determine node duplication? I am afraid of errors in string hash, so I use a set to store the status! Let's just say nothing about set ......
Some details:
1. note that the rules in the question may be entered with multiple targets, such as ABC-> def, Abd-> 123. When I started using map to store these rules, I certainly encountered an error, the solution is simple: multimap and multimap insert, delete, and search are similar to map, and they are all different. For example, you cannot use subscript for insert, but you need to use the insert member function! Deleting is to delete all elements with the same keywords. If it is found during search, only the first iterator of the same keyword is returned ......
2. The State array is used to store the State (because the set will be automatically sorted, it will disrupt the order of the State to be calculated, so you have to store one more array ). How can we determine the size of the array? I will not calculate ...... The second submission burst ...... Well, you have to keep up with mathematics. You can't just learn algorithm ideas!
#include <iostream>#include <string>#include <map>#include <set>using namespace std;string goal;multimap<string, string> M;map<string, int> N;set<string> col;const int Max = 900000;//?how manystring state[Max];void Init();int bfs();int main(){Init();int ans = bfs();if(ans == -1) cout << "NO ANSWER!" << endl;else cout << ans << endl;return 0;}void Init(){cin >> state[0] >> goal;string p, q;while(cin >> p >> q)M.insert(make_pair(p, q));}int bfs(){int head = 0, tail = 1;size_t pos;N[state[head]] = 0;map<string, string>::iterator it;while(head < tail){if(N[state[head]] > 10){++head;continue;}if(state[head] == goal){if(N[goal] > 10) return -1;return N[goal];}for(it=M.begin(); it!=M.end(); ++it){state[tail] = state[head];pos = state[tail].find(it->first);while(pos != string::npos){state[tail].replace(pos, it->first.size(), it->second);if(col.find(state[tail]) == col.end()){col.insert(state[tail]);N[state[tail]] = N[state[head]] + 1;++tail;}state[tail] = state[head];pos = state[tail].find(it->first, pos+it->first.size());}}++head;}return -1;}
Breadth-first search-string replacement