In fact, it is the linked list of the intersection:
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <unordered_map>5 6 using namespacestd;7 8 classNode {9 Public:TenNode (): Data (0), Next (0) {} One Chardata; A intNext; - }; - the voidPrint_list (unordered_map<int, node*>& Mem,inthead) { - intCur =head; - while(cur! =-1) { -cout<<mem[cur]->data<<" "; +Cur = mem[cur]->Next; - } +cout<<Endl; A }; at - intStep_list (unordered_map<int, node*>& Mem,int from,intN) { - if(N <0)return-1; - intCur = from; - while(cur! =-1) { - if(n = =0) { in Break; - } toCur = mem[cur]->Next; +n--; - } the returncur; * } $ Panax Notoginseng intCount_list (unordered_map<int, node*>& Mem,int from) { - if( from<0)return 0; the intCur = from; + intCNT =0; A while(cur! =-1) { theCur = mem[cur]->Next; +cnt++; - } $ returnCNT; $ } - - intMain () { the inthead_a, head_b; - intN;Wuyi theunordered_map<int, node*>Mem; - WuCin>>head_a>>head_b>>N; - About for(intI=0; i<n; i++) { $ intaddr; -node* NP =NewNode (); -scanf"%d%c%d", &addr, & (Np->data), & (np->next)); - Mem.insert (Make_pair (addr, NP)); A } + the intCnt_a =count_list (Mem, head_a); - intCnt_b =count_list (Mem, head_b); $ the intCnt_diff = cnt_a-Cnt_b; the intHead_long =head_a; the intHead_short =head_b; the if(Cnt_diff <0) { -Cnt_diff =-Cnt_diff; inHead_long =head_b; thehead_short=head_a; the } About theHead_long =step_list (Mem, Head_long, Cnt_diff); the the while(Head_long! =Head_short) { +Head_long = mem[head_long]->Next; -Head_short= mem[head_short]->Next; the }Bayi if(Head_long <0) { theprintf"-1\n"); the}Else { -printf"%05d\n", Head_long); - } theSystem"Pause"); the return 0; the}
PAT 1032. Sharing