PAT 1032. Sharing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.