给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人。

输入格式:

输入首先给出正整数NN(\le 10^5≤10​5​​),为通话记录条数。随后NN行,每行给出一条通话记录。简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。

输出格式:

在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数。

输入样例:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832

输出样例:

13588625832 3
搜索树程序:
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long LL;
typedef struct Treenode
{
LL str;
int cnt;
struct Treenode* left;
struct Treenode* right;
}Treenode,*Tree;
Tree Newnode(LL s)
{
Tree t = (Tree)malloc(sizeof(Treenode));
t->str = s;
t->cnt = ;
t->left = NULL;
t->right = NULL;
return t;
}
Tree Insert(Tree T,LL s)
{
if(!T)
T = Newnode(s);
else if(T->str>s)
T->left = Insert(T->left,s);
else if(T->str<s)
T->right = Insert(T->right,s);
else
T->cnt = T->cnt+;
return T;
}
void find(Tree T,LL &mins,int &maxt,int &same)
{
if(T)
{
if(T->cnt > maxt)
{
maxt = T->cnt;
mins = T->str;
same = ;
}
else if((T->cnt==maxt))
{
if(T->str<mins)
mins = T->str;
same++;
}
find(T->left,mins,maxt,same);
find(T->right,mins,maxt,same);
}
}
int main()
{
int n;
LL t1;
Tree T;
cin>>n;
cin>>t1;
T =Newnode(t1);
for(int i=;i<*n;i++)
{
cin>>t1;
T = Insert(T,t1);
}
LL ms=;
int mt=,num=;
find(T,ms,mt,num);
cout<<ms<<' '<<mt;
if(num>)
cout<<' '<<num<<endl;
else
cout<<endl;
return ;
}

hash程序

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

/*
Hash表的使用方法//hash表size一般是大于N的第一个素数
1.嵌套定义结构体 注意在hashtable 中定义的是一个指向指针的指针(指向list结构体的指针列表)
初始化时注意分配空间!
所以在给Hshlist分配空间的时候,要注意,(list*)malloc(sizeof(list))这样才对!
2.确定hash函数,在这里使用atoi把字符串转变为数字然后对哈希表size取余数.
3.插入操作,查找操作
列表元素第一个是头节点,应该从它的next元素开始遍历
通过hash函数找到对应的在list中的位置,然后在该位置中查找元素,如果存在就计数,不存在就在列表头
加入元素.
4.destroy操作,分别对应Init一一释放空间
*/

typedef struct Listnode *Pos;
typedef struct Listnode
{
char Phonenum[];
int cnt;
Pos next;
}*List;
typedef struct Hashtable
{
int tablesize;
List *Thelist;
} *HashTable; int Hash(int key,int size)
{
return key%size;
}
void Insert(char key[],HashTable H);
Pos Find(char key[],HashTable H);
HashTable Init(int size);
void destroy(HashTable H);
void FindMax(HashTable H);
int NextPrime(int x);
int main()
{
int n;
scanf("%d",&n);
HashTable H = Init(*n);
char from[],to[];
for(int i=;i<n;i++)
{
scanf("%s%s",from,to);
Insert(from,H);
Insert(to,H);
}
FindMax(H);
destroy(H);
}
int NextPrime(int x)
{
int i,j;
for(i=x;;i++)
{
for(j=;j*j<=i;j++)
if(i%j==)
break;
if(j*j>i)
return i;
}
}
HashTable Init(int size)
{
int Tsize = NextPrime(size);
HashTable H = (HashTable)malloc(sizeof(Hashtable));
H->tablesize = Tsize;
H->Thelist = (List*)malloc(sizeof(List)*Tsize);
for(int i=;i<Tsize;i++)
{
H->Thelist[i] = (List)malloc(sizeof(Listnode));
H->Thelist[i]->next = NULL;
}
return H;
} void Insert(char key[],HashTable H)
{
Pos p,tmp;
List t = H->Thelist[Hash(atoi(key+),H->tablesize)];
p = Find(key,H);
if(p==NULL)
{
tmp = (List)malloc(sizeof(Listnode));
tmp->cnt = ;
strcpy(tmp->Phonenum,key);
tmp->next = t->next;
t->next = tmp;
}
else
(p->cnt)++;
} Pos Find(char key[],HashTable H)
{
List t = H->Thelist[Hash(atoi(key+),H->tablesize)];
List p = t->next;
while(p!=NULL&&strcmp(p->Phonenum,key))
p = p->next;
return p;
} void destroy(HashTable H)
{
for(int i=;i< H->tablesize;i++)
{
free(H->Thelist[i]);
}
free(H->Thelist);
free(H);
} void FindMax(HashTable H)
{
char MinPhone[];
int max = -,same = ;
for(int i=;i<H->tablesize;i++)
{
List t = H->Thelist[i];
t = t->next;
while(t!=NULL)
{
if( t->cnt > max)
{
max = t->cnt;
strcpy(MinPhone,t->Phonenum);
same = ;
}
else if(t->cnt == max)
{
if(strcmp(MinPhone,t->Phonenum)>)
strcpy(MinPhone,t->Phonenum);
same++;
}
t =t->next;
}
}
printf("%s %d", MinPhone, max);
if (same > )
printf(" %d", same);
}

这题一开始用map超时,然后我试了试二叉搜索树,也无情超时,看来必须用hash

最新文章

  1. 使用 Wireshark 调试 HTTP/2 流量
  2. 爱上MVC~在Views的多级文件夹~续~分部页的支持
  3. MFC中控制COMBOBOX控件的下拉框高度
  4. Weka 入门1
  5. 小SQL大作用
  6. 基于visual Studio2013解决C语言竞赛题之0407最大值最小值
  7. zTree实现地市县三级级联报错(三)
  8. 开源博客系统使用springmvc
  9. Tomcat启动报错,报找不到gdk_custom.jar
  10. 第27月第18天 epoll lt et
  11. Kafka获取订阅某topic的所有consumer group【客户端版】
  12. 【转载】关于nginx以及内核参数的配置
  13. python开发计算器
  14. 最小重组缓冲区和路径MTU发现
  15. PrefixHeader.pch 在工程中的使用
  16. C语言函数參数传递原理
  17. VS+SVN版本控制
  18. C# 键盘中的按键对应KeyValue
  19. 20145329 《Java程序设计》课程总结
  20. Yasm 1.3.0 Release Notes

热门文章

  1. Net 发布网站中遇到的几点问题
  2. daily_journal_3 the game of thrones
  3. H - Where is the Marble?(set+vector)
  4. EditText(2)自定义回车键的行为
  5. 全面学习ORACLE Scheduler特性(5)Schedules调度Programs执行的Jobs
  6. jquery中有关cookie的使用简要说明
  7. leetcode486 Predict the Winner
  8. java虚拟机(四)--内存溢出、内存泄漏、SOF
  9. 浅谈:nodejs在cmd提示不是内部或外部命令
  10. 散列(hash)