PTA 5-14 電話聊天狂人 (25分)


給定大量手機用戶通話記錄,找出其中通話次數最多的聊天狂人。

輸入格式:

輸入首先給出正整數NN(\le 10^5105​​),為通話記錄條數。隨后NN行,每行給出一條通話記錄。簡單起見,這里只列出撥出方和接收方的11位數字構成的手機號碼,其中以空格分隔。

輸出格式:

在一行中給出聊天狂人的手機號碼及其通話次數,其間以空格分隔。如果這樣的人不唯一,則輸出狂人中最小的號碼及其通話次數,並且附加給出並列狂人的人數。

輸入樣例:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832

輸出樣例:

13588625832 3

/*
 * 就這道題而言:如果采取直接電話號碼長度為11 直接排序O(11 * n * log n ) 然后在找出現最多的O(11 * n) 時間效率比采用散列表還要好點- -
 1.上述方案在實際應用中不可行  應為會涉及到不斷插入新號碼的問題 每插入一個新號碼都要重新進行排序。  
 可以考慮用散列表解決  這題哈希函數采用除留余數法 解決沖突采用分離鏈接法。
*/
#include "iostream"
#include "cmath"
#include "cstring"
using namespace std;
#define KEYLENGTH 11
typedef char ElementType[KEYLENGTH];
typedef int Index; /* 散列地址類型 */

struct LNode {
    ElementType data;
    LNode* next;
    int count;
};
typedef LNode *ptrToLNode;
typedef ptrToLNode Position;
typedef ptrToLNode List;
typedef struct TblNode* HashTable;
struct TblNode {   /* 散列表節點定義 */
    int tableSize; /* 表的大小 */
    List heads;    /* 指向鏈表頭結點的數組 */
};
#define MAXTABLESIZE 1000000
int nextPrime(int n) {
    int i, 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;
        else
            p += 2;
    }
    return p;
}
HashTable createTable(int tableSize) {
    HashTable h;
    int i;
    h = (HashTable)malloc(sizeof(struct TblNode));
    h->tableSize = nextPrime(tableSize);
    h->heads = (List)malloc(h->tableSize* sizeof(struct LNode));
    for (i = 0; i < h->tableSize; i++) {
        h->heads[i].data[0] = '\0'; h->heads[i].next = NULL;
        h->heads[i].count = 0;
    }
    return h;
}

int  Hash(int p, int tableSize) { /* 設置映射的哈希函數 */
    return p % tableSize; /* 采用除留余數法 */
}

Position find(HashTable h , ElementType key) {
    Position p;
    Index pos;
    pos = Hash(atoi(key+KEYLENGTH-5),h->tableSize); /* 初始散列位置 */
    p = h->heads[pos].next;  /* 從該鏈表的第一個節點開始 */
    while (p && strcmp(p->data, key)) /* 未到表尾  並且key未找到時 */
        p = p->next;
    return p;
}

bool insert(HashTable h, ElementType key) {
    Position p, newCell;
    Index pos;
    p = find(h, key);
    if (!p) {
        newCell = (Position)malloc(sizeof(struct LNode));
        strcpy(newCell->data, key);
        newCell->count = 1;
        pos = Hash(atoi(key + KEYLENGTH - 5), h->tableSize); /* 初始散列位置 */
        /* 頭插法  將newCell作為h->heads[pos]的第一個結點 */
        newCell->next = h->heads[pos].next;
        h->heads[pos].next = newCell;
        return true;
    }
    else {
        p->count++;
        return false;
    }
}

void scanAndOutput(HashTable h) {
    int i, MaxCnt, PCnt;
    MaxCnt = PCnt = 0;
    ElementType MinPhone;
    List ptr;
    MinPhone[0] = '\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;
}

void destroyTable(HashTable h) {
    int i;
    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);
}
int main() {
    int n, 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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM