11-散列1 电话聊天狂人 (25分)

//字符串排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[200005][12];

int cmp(const void *a, const void *b) {
    int k = strcmp( (const char *)a, (const char*) b);
    if(k >= 0) return 1;
    else return -1;
}

int main() {
    int N, i, j, Mcnt = 1, cnt = 1, k = 1;
    scanf("%d", &N);
    for(i = 0; i < 2*N; i++) scanf("%s", str[i]);
    qsort(str, 2*N, 12*sizeof(char), cmp);
    //for(i = 0; i < 2*N; i++) printf("%s\n", str[i]);
    for(i = 0; i < 2*N; i++) {
        if(!strcmp(str[i+1], str[i])) cnt++;
        else {
            if(cnt > Mcnt) {
                Mcnt = cnt;
                j = i;
                k = 1;
            }
            else if(cnt == Mcnt) k++;
            cnt = 1;
        }
    }
    printf("%s %d", str[j], Mcnt);
    if(k > 1) printf(" %d\n", k);
    return 0;
}

//哈希表
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define Length 6

typedef struct LNode *PtrToNode;
struct LNode {
    int cnt;
    char tele[12];
    PtrToNode Next;
};
typedef PtrToNode List;

typedef struct TblNode *HashTable;
struct TblNode {
    int TableSize;
    List Head;
};

int Hash(int Key, int p) {
    return Key % p;
}

int NextPrime(int N) {
    int p = N+1, i;
    while(1) {
        for(i = (int)sqrt(p); i > 2; i--)
            if(p%i == 0) break;
        if(i == 2) break;
        else p += 2;
    }
    return p;
}

HashTable Create(int N) {
    int i;
    HashTable H = (HashTable)malloc(sizeof(struct TblNode));
    H->TableSize = NextPrime(N);
    H->Head = (PtrToNode)malloc(H->TableSize*sizeof(struct LNode));
    for(i = 0; i < H->TableSize; i++) {
        H->Head[i].Next = NULL;
        H->Head[i].tele[0] = 0;
        H->Head[i].cnt = 0;
    }
    return H;
}

PtrToNode Find(HashTable H, char Key[]) {
    PtrToNode p;
    int pos;
    pos = Hash(atoi(Key+Length), H->TableSize);
    p = H->Head[pos].Next;
    while(p && strcmp(p->tele, Key)) p = p->Next;
    return p;
}

void Insert(HashTable H, char Key[]) {
    PtrToNode p, newnode;
    int pos;
    p = Find(H, Key);
    if(!p) {
        newnode = (PtrToNode)malloc(sizeof(struct LNode));
        strcpy(newnode->tele, Key);
        pos = Hash(atoi(Key+Length), H->TableSize);
        newnode->cnt = 1;
        newnode->Next = H->Head[pos].Next;
        H->Head[pos].Next = newnode;
    }
    else {
        p->cnt++;
    }
}

void Print(HashTable H) {
    int i;
    PtrToNode p;
    //printf("H->size = %d\n", H->TableSize);
    for(i = 0; i < H->TableSize; i++) {
        p = H->Head[i].Next;
        while(p) {
            printf("%d %s", p->cnt, p->tele);
            p = p->Next;
            printf("\n");
        }
    }
}

void Solve(HashTable H) {
    int Mcnt = 0, num, i;
    char ans[12];
    PtrToNode p;
    memset(ans, 0, sizeof(ans));
    for(i = 0; i < H->TableSize; i++) {
        p = H->Head[i].Next;
        while(p) {
            if(p->cnt > Mcnt) {
                Mcnt = p->cnt;
                strcpy(ans, p->tele);
                num = 1;
            }
            else if(p->cnt == Mcnt) {
                if(strcmp(ans, p->tele) > 0)
                    strcpy(ans, p->tele);
                num++;
            }
            p = p->Next;
        }
    }
    printf("%s %d", ans, Mcnt);
    if(num > 1) printf(" %d\n", num);
}

int main() {
    int N;
    int i;
    HashTable H;
    char tele[12];
    scanf("%d", &N);
    H = Create(2*N);
    for(i = 0; i < 2*N; i++) {
        scanf("%s", tele);
        Insert(H, tele);
    }//Print(H);
    Solve(H);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值