Send the contact information in the comments to test the genealogy data.
Genealogy (gen) Time Limit 2 s [Problem description] modern people are more and more interested in the family lineage, and now there are plenty of parent-child relationships, please write a program to find someone's earliest ancestor. [Input format] gen. the in input file consists of multiple lines. The first is a series of descriptions about the parent-child relationship. Each group of parent-child relationships consists of two lines, use # name to describe the name of the father in a group of parent-child relationships, and use + name to describe the name of the Son in a group of parent-child relationships. Then use? Name indicates the earliest ancestor of the person to be requested, and a single $ indicates the end of the file. It is required that each person's name has only six characters, and the first letter is uppercase, and no two people have the same name. A maximum of 1000 parent-child relationships may exist, and the total number may reach 50000. The records in the family tree cannot exceed 30 generations. [Output format] gen. out obtains the ancestor of each person who is looking for the ancestor in the order required by the input file. Format: My name + A space + ancestor name + carriage return. [Input example] # George + rosydney # Arthur + Gareth + Walter # Gareth + Edward? Edward? Walter? Rosydney? Arthur $ [Output example] Edward Arthur Walter Arthur rosydney George Arthur's idea: the core idea is a bit similar to the query set. It is easier to use map for implementation. Be sure to grasp the logical relationship, disadvantages of classification discussion: Under this Code, the debugging function of Dev c ++ is virtually empty.
1 # include <iostream> 2 # include <map> 3 # include <cstdio> 4 # include <cstring> 5 using namespace std; 6 int f [101]; 7 string fanow; // store the name of the current parent to facilitate the addition of the child node 8 map <string, string> p; 9 string name; 10 int main () 11 {12 char; 13 while (cin> a) 14 {15 if (a = '$') 16 break; 17 if (a = '#') // father name 18 {19 cin> name; 20 if (p [name]. empty () 21 p [name] = name; 22 fanow = name; 23} 24 if (a = '+ ') // child name 25 {26 cin> name; 27 while (p [fa Now]! = Fanow) 28 {29 fanow = p [fanow]; 30} 31 p [name] = fanow; 32} 33 if (a = '? ') // Ask 34 {35 cin> name; 36 cout <name <"" <p [name] <endl; 37} 38} 39 return 0; 40}