首页 > 代码库 > 家谱(gen)——洛谷P2814

家谱(gen)——洛谷P2814

 1 #include <iostream>  
 2 #include <string> 
 3 #include <map>
 4 using namespace std;  
 5 map < string, string > mp;  
 6 int main()  
 7 {   
 8     string s1 = "", s2 = "";  
 9     while(cin >> s1)  
10     {  
11         mp.clear();  
12         while(s1 != "$")  
13         {  
14             if(s1[0] == #)  
15             {  
16                 s2 = s1.substr(1,s1.size()-1);  
17                 if(mp[s2] == "") mp[s2] = s2;  
18             }  
19             if(s1[0] == +)  
20             {  
21                 s1 = s1.substr(1,s1.size()-1);  
22                 while(mp[s2] != s2) s2 = mp[s2];  
23                 mp[s1] = s2;  
24             }  
25             if(s1[0] == ?)  
26             {  
27                 s1 = s1.substr(1,s1.size()-1);  
28                 string s3 = s1;  
29                 while(s3 != mp[s3]) s3 = mp[s3];  
30                 cout << s1 <<   << s3 << endl;  
31             }  
32             cin >> s1;  
33         }  
34     }  
35     return 0;  
36 }

充斥着STL的代码,感觉自己STL依赖症到了一个新的境界。。。

是个并查集(然而并没有看出来),用map控制映射。→_→

家谱(gen)——洛谷P2814