PTA 5-14 Phone Chat Madman (25 points)

Source: Internet
Author: User
Tags strcmp

Given a large number of mobile phone users call logs, find the most calls in the chat madman.

Input format:

The input first gives a positive integerNN (\le 10^5≤10? ) 5?? ) for the number of call logs. Then nn rows, each row gives a call record. For simplicity, only the 11-digit mobile number of the dial-out party and the receiver is listed here, separated by a space.

Output format:

The phone number of the chat madman and the number of calls are given in a row, separated by a space. If such a person is not unique, then output the smallest number in the madman and its number of calls, and attach to the number of people who are tied to the madman.

Input Sample:
413005711862 1358862583213505711862 1308862583213588625832 1808792583215005713862 13588625832
Sample output:
13588625832 3

/** In terms of this problem: if take direct phone number length of 11 direct sort O (one * n * log n) and then find the most of the O (one * n) time efficiency than the use of a hash list is better-1. The above scheme in practical application should be involved in the continuous insertion of the new number of questions   Each insertion of a new number will be reordered. Consider using a hash table to solve this problem hash function using the separation of remainder method to resolve the conflict using the separate link method. */#include"iostream"#include"Cmath"#include"CString"using namespacestd;#defineKeylength 11typedefCharElementtype[keylength];typedefintIndex;/*Hash Address Type*/structLnode {ElementType data; Lnode*Next; intcount;}; typedef lnode*ptrtolnode;typedef ptrtolnode position;typedef ptrtolnode list;typedefstructtblnode*HashTable;structTblnode {/*Hash Table node definition*/    intTablesize;/*Size of Table*/List heads; /*array that points to the node of the linked header*/};#defineMaxtablesize 1000000intNextPrime (intN) {intI, p = (n%2) ? n +2: N +1;  while(P <maxtablesize) {         for(i = sqrt (p); i >2; i--)            if(! (p%i)) Break; if(i = =2)             Break; ElseP+=2; }    returnp;} HashTable CreateTable (inttablesize)    {HashTable h; inti; H= (HashTable)malloc(sizeof(structTblnode)); H->tablesize =NextPrime (tablesize); H->heads = (List)malloc(h->tablesize*sizeof(structLnode));  for(i =0; I < h->tablesize; i++) {h->heads[i].data[0] =' /'; H->heads[i].next =NULL; H->heads[i].count =0; }    returnh;}intHash (intPintTablesize) {/*set a hash function for a map*/    returnP% Tablesize;/*adopting the method of remainder residue*/}position Find (HashTable H, ElementType key) {Position p;    Index POS; POS= Hash (Atoi (key+keylength-5), h->tablesize);/*Initial Hash position*/P= h->heads[pos].next;/*start from the first node of the list*/     while(P && strcmp (P->data, key))/*not at the end of the table and key is not found*/P= p->Next; returnp;}BOOLInsert (HashTable h, ElementType key) {Position p, Newcell;    Index POS; P=find (H, key); if(!p) {Newcell= (Position)malloc(sizeof(structLnode)); strcpy (Newcell-data, key); Newcell->count =1; POS= Hash (atoi (key + Keylength-5), h->tablesize);/*Initial Hash position*/        /*The first node of the Newcell as the H->heads[pos] by the head interpolation method*/Newcell->next = h->Heads[pos].next; H->heads[pos].next =Newcell; return true; }    Else{p->count++; return false; }}voidscanandoutput (HashTable h) {intI, maxcnt, pcnt; Maxcnt= PCNT =0;    ElementType Minphone;    List ptr; minphone[0] =' /';  for(i =0; I < h->tablesize; i++) {ptr= h->Heads[i].next;  while(PTR) {if(Ptr->count >maxcnt) {maxcnt= ptr->count; strcpy (Minphone, PTR-data); Pcnt=1; }            Else if(Ptr->count = =maxcnt) {pcnt++; if(strcmp (Minphone, Ptr->data) >0) strcpy (Minphone, PTR-data); } ptr= ptr->Next; }} cout<< Minphone <<" "<<maxcnt; if(Pcnt >1) cout<<" "<<pcnt; cout<<Endl;}voiddestroytable (HashTable h) {inti;    Position p, temp;  for(i =0; I < h->tablesize; i++) {p= h->Heads[i].next;  while(P! =NULL) {Temp= p->Next;  Free(P); P=temp; }    }     Free(h->heads);  Free(h);}intMain () {intN, I;    ElementType key; CIN>>N; HashTable h= CreateTable (2*N);  for(i =0; I < n; i++) {cin>>key; Insert (h, key); CIN>>key; Insert (h, key);    } scanandoutput (h);    Destroytable (h); return 0;}

PTA 5-14 Phone Chat Madman (25 points)

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.