poj-1002:487-3279 detailed 1: Heap sorting method

Source: Internet
Author: User

> Analysis>> The difficulty lies in the speed of sorting>> sorting algorithm to consider a lot of duplicates, no duplicates two cases>> of course, because the subject of memory consumption is not high, you can also do not use sorting 

> General Ideas

>> Save phone numbers in the order you enter them first>> use heap sorting for all numbers>> number of consecutive frequency output frequencies greater than 1 after sorting according to the number > Input Conversion>> the mapping of letters to numbers has been defined in detail, so it is recommended to use a faster mapping table > Storage Format>> because it involves sorting comparisons, it is recommended to store the phone number as an integer, without using a string > Sort>> It is recommended to use heap sorting, considering that there may be many duplicate numbers>> Quick Sort efficiency soles when too many duplicates are processed > attached Code
/*---------------------------------* Heap sequencing, output calculation occurs at the frequency *---------------------------------*/#include"stdio.h"#include"string.h"#include"stdlib.h"/*letter-to-number mapping table*/Const intmap[ -] = {    2,2,2,/*A, B, C*/    3,3,3,/*D, E, F*/    4,4,4,/*G, H, I*/    5,5,5,/*J, K, L*/    6,6,6,/*M, N, O*/    7,0,7,7,/*P, Q, R, S*/    8,8,8,/*T, U, V*/    9,9,9,/*W, X, Y*/    0           /*Z*/} ;/*store all the phone numbers that appear*/inttelenum[100000] = {0} ;voidExchangeint*P1,int*p2) {    intTMP = *P1; *P1 = *P2; *P2 =tmp;}/*maintain the nature of the maximum heap (non-recursive)*/voidMaxheapify (int*pstart,intSizeinti) {    intleft =0 ; intright =0 ; intlargest =0 ; Largest=i;  Do{i=largest; Left=2* i +1 ; Right=2* i +2 ; if(Left < size && Pstart[left] >Pstart[largest]) largest=Left ; if(Right < size && Pstart[right] >Pstart[largest]) largest=Right ; if(Largest! =i) Exchange (&pstart[i], &Pstart[largest]); } while(Largest! =i);}/*Heap Sort*/voidSortint*pstart,intsize) {    inti =0 ;  for(i = size/2-1; I >=0; i--) maxheapify (pstart, size, i);  for(i = size-1; i >0; i--) {Exchange (&pstart[0], &Pstart[i]); Maxheapify (Pstart, I,0) ; }    }intMainvoid){    intn =0 ; Chars[ -] = {0} ; intnum =0 ; inti =0, j =0 ; intCount =0 ; CharFlag =0 ; scanf ("%d", &N);  for(i =0; I < n; i++) {memset (s),0,sizeof(s)); scanf ("%s", s); /*Convert a phone number to an integer type*/Num=0 ;  for(j =0; J <sizeof(s); J + +)        {            if(0==S[j]) Break ; if('Q'= = S[j] | |'Z'==S[j])Continue ; if(S[j] >='A'&& S[j] <='Z') Num= num *Ten+ Map[s[j]-'A'] ; if(S[j] >='0'&& S[j] <='9') Num= num *Ten+ S[j]-'0' ; } Telenum[i]=num;    } sort (Telenum, n); Flag=0 ; Num= telenum[0] ; Count=1 ;  for(i =1; I < n; i++)    {        if(Telenum[i]! =num) {            if(Count >1) {flag=1 ; printf ("%03d-%04d%d\r\n", NUM/10000, Num%10000, Count); } num=Telenum[i]; Count=1; }        ElseCount++ ; }    if(Count >1) {flag=1 ; printf ("%03d-%04d%d\r\n", NUM/10000, Num%10000, Count); }    if(0==flag) printf ("No duplicates.\r\n") ; return 0 ;}

poj-1002:487-3279 detailed 1: Heap sorting method

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.