首页 > 代码库 > hiho1062

hiho1062

题目链接:https://hihocoder.com/problemset/problem/1062

不会。。。

原文:http://www.cnblogs.com/SeekHit/p/6527546.html

 

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<string>
 4 #include<map>
 5 using namespace std;
 6 string a,b;
 7 map<string ,int> vis;
 8 map<string, string> fa;
 9 
10 void fin(string a,string b)
11 {
12     vis.clear();
13     string x=a;
14     while(!x.empty())
15     {
16         vis[x]=1;
17         x=fa[x];
18     }
19 
20     x=b;
21     while(!x.empty()&&!vis[x])
22     {
23         x=fa[x];
24     }
25     if(!x.empty()) cout<<x<<endl;
26         else cout<<-1<<endl;
27 
28 
29 }
30 int main()
31 {
32     int n,m;
33     cin>>n;
34     while(n--)
35     {
36         cin>>a>>b;
37         fa[b]=a;
38     }
39     cin>>m;
40     while(m--)
41     {
42         cin>>a>>b;
43         fin(a,b);
44     }
45 }

 

hiho1062