11-散列1 电话聊天狂人 (25分)
来源:互联网 发布:91助手苹果mac版下载 编辑:程序博客网 时间:2024/04/30 04:40
//字符串排序#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 6typedef 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
- 11-散列1 电话聊天狂人 (25分)
- 11-散列1 电话聊天狂人 (25分)
- 11-散列1 电话聊天狂人 (25分)
- 11-散列1 电话聊天狂人 (25分)
- 11-散列1 电话聊天狂人(25 分)
- 11-散列1 电话聊天狂人
- 11-散列1 电话聊天狂人
- 11-散列1 电话聊天狂人 (25分)
- 电话聊天狂人(25 分)(散列函数)
- 哈希表的使用-11-散列1 电话聊天狂人
- 5-20 电话聊天狂人 (25分)
- 5-14 电话聊天狂人 (25分)
- 5-14 电话聊天狂人 (25分)
- 电话聊天狂人(25 分)
- MOOC-数据结构 5-14 电话聊天狂人 (25分)
- 5-14 电话聊天狂人 (25分)/PTA
- 7-20 电话聊天狂人(25 分) (哈希)
- PAT 电话聊天狂人
- [UESTC577]分数拆分 dfs迭代加深
- javaweb设计模式
- linux下的single()函数
- Item41 Consider pass by value for copyable parameters that are cheap to move and always copied.
- JavaScript对象2
- 11-散列1 电话聊天狂人 (25分)
- 阻塞队列BlockingQueue
- JavaUtils-递归解析多级菜单(数据树)
- onNewIntent是个什么东西?
- [Unity3D 版本5.X]实现一个自由漫游的摄像机
- 悬浮按钮和可交互提示
- Android的android:width和android:layout_width
- 网易大数据面试题试解
- R语言预测初步(R语言预测实战-节选)