7-20 电话聊天狂人(25 分)(Hash模板)

 

7-20 电话聊天狂人(25 分)

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

输入格式:

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

输出格式:

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

输入样例:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832

输出样例:

13588625832 3

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define KEYLENGTH 11	    //关键词字符串最大长度
#define MAXTABLESIZE 1000000    //允许开辟的最大散列表长度
#define MAXD 5      //参与散列映射计算的字符个数
typedef char ElementType[KEYLENGTH + 1];
typedef int Index; //散列地址类型

//单链表定义
typedef struct LNode *PtrToLNode;
struct LNode {
	ElementType Data;
	PtrToLNode Next;
	int Count;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

//散列表结点定义
typedef struct TblNode *HashTable;	//散列表类型
struct TblNode {
	int TableSize; //表的最大长度
	List Heads;	   //指向链表头结点的数组
};

int NextPrime( int N ) {
	//返回大于N且不超过MAXTABLESIZE的最小素数
	int i, p = ( N % 2 ) ? N + 2 : N + 1;	//从大于N的下一个奇数开始
	while ( p <= MAXTABLESIZE ) {
		double q = p;
		for ( i = (int)sqrt(q); i > 2; i-- )
			if ( !(p % i) ) break; //p不是素数
		if ( i == 2 ) break; // for正常结束,说明p是素数
		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;    //链表为NULL
		H->Heads[i].Count = 0;  //对应号码的个数为0
	}
	return H;   //最后将表头结点数组的首地址返回
}

//hash函数
Index Hash ( const char *Key, int TableSize ) {
	unsigned int h = 0; //散列函数值,初始化为0
	while ( *Key != '\0' ) //位移映射
		h = ( h << 5 ) + *Key++;
	return h % TableSize;
}

Position Find ( HashTable H, ElementType Key ) {
	Position P;
	Index Pos;
	Pos = Hash( Key + KEYLENGTH - MAXD, H->TableSize ); //初始散列位置(利用hash函数快速定位)
	P = H->Heads[Pos].Next;     //从该链表的第1个结点开始
	while ( P && strcmp( P->Data, Key ) )   //寻找是否有Key,退出的条件:P为NULL, 或者是找到了
		P = P->Next;

	return P;   //此时P或者指向找到的结点,或者为NULL
}

int Insert ( HashTable H, ElementType Key ) {
	Position P, NewCell;
	Index Pos;
	P = Find( H, Key ); //先定位:找到or找不到
	if ( !P ) { //关键词未找到,可以插入(声明临时节点存储待插入的数据,然后利用hash函数找到对应的位置,之后是链表的头插法)
		NewCell = (Position)malloc(sizeof(struct LNode));
		strcpy(NewCell->Data, Key);
		NewCell->Count = 1; //个数+1
		Pos = Hash( Key + KEYLENGTH - MAXD, H->TableSize ); //初始散列位置
		//将NewCell插入为H->Heads[Pos]链表的第一个结点(头插法)
		NewCell->Next = H->Heads[Pos].Next;
		H->Heads[Pos].Next = NewCell;

		return 1;
	}
	else { //关键词已存在
		P->Count++;
		return 0;
	}
}

void DestroyTable( HashTable H ) {  //释放空间
	int i;
	Position P, Tmp;
	//释放每个链表的结点
	for( i = 0; i < H->TableSize; i++ ) {
		P = H->Heads[i].Next;
		while ( P ) {   //依次释放链表每一个元素的空间
			Tmp = P->Next;
			free( P );
			P = Tmp;
		}
	}
	free( H->Heads ); //释放头结点数组
	free( H );		  //释放散列表头结点
}

void ScanAndOutput ( HashTable H ) {
	int i, MaxCnt = 0, PCnt = 0;
	ElementType MinPhone;
	List Ptr;
	MinPhone[0] = '\0';
	for ( i = 0; i < H->TableSize; i++ ) { //扫描链表
		Ptr = H->Heads[i].Next; //从该链表的第1个结点开始
		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;
		}
	}
	printf("%s %d", MinPhone, MaxCnt);
	if ( PCnt > 1 )
		printf(" %d", PCnt);
	printf("\n");
}
int main () {
	int N, i;
	ElementType Key;
	HashTable H;
	scanf("%d", &N);
	H = CreateTable( N * 2 );	//创建一个散列表
	for ( i = 0; i < N; i++ ) {
		scanf("%s", Key); Insert( H, Key );
		scanf("%s", Key); Insert( H, Key );
	}
	ScanAndOutput( H );
	DestroyTable( H );

	return 0;
}

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C语言中实现7-4题,即词频统计,可以通过使用哈希表的方式来解决。哈希表是根据关键字直接访问存储位置的数据结构,它通过计算关键字的哈希值,将其映射到某个数组或链表的位置,实现高效的查找和插入操作。 具体实现步骤如下: 1. 定义一个哈希表,作为存储词频的数据结构。可以使用数组和链表的结合来实现,每个数组元素存储一个链表。 2. 读取输入的字符串。 3. 遍历字符串,依次将每个单词提取出来。可以通过空格或标点符号来隔单词。 4. 对每个单词进行哈希计算,得到哈希值。 5. 在哈希表中查找对应哈希值的位置,如果已存在则更新词频,否则插入新的节点。 6. 输出哈希表中每个单词的词频。 以下是一个简单的示例代码: ``` #include <stdio.h> #include <string.h> #include <ctype.h> // 哈希表节点 typedef struct Node { char word[100]; // 单词 int frequency; // 词频 struct Node* next; // 链表指针 } Node; #define HASH_SIZE 1000 // 哈希表大小 Node* hashTable[HASH_SIZE]; // 哈希表数组 // 计算哈希值 int hash(char* word) { int sum = 0; for (int i = 0; i < strlen(word); i++) { sum += word[i]; } return sum % HASH_SIZE; } // 在哈希表中插入或更新节点 void insertOrUpdate(char* word) { int index = hash(word); Node* node = hashTable[index]; while (node != NULL) { if (strcmp(node->word, word) == 0) { // 单词已存在,更新词频 node->frequency++; return; } node = node->next; } // 单词不存在,插入新节点 Node* newNode = (Node*) malloc(sizeof(Node)); strcpy(newNode->word, word); newNode->frequency = 1; newNode->next = hashTable[index]; hashTable[index] = newNode; } // 输出哈希表中的单词和词频 void printFreq() { for (int i = 0; i < HASH_SIZE; i++) { Node* node = hashTable[i]; while (node != NULL) { printf("%s: %d\n", node->word, node->frequency); node = node->next; } } } int main() { // 初始化哈希表 memset(hashTable, 0, sizeof(hashTable)); char input[10000]; scanf("%[^\n]", input); // 读取输入的字符串直到换行符 char* token = strtok(input, " ,.-"); // 使用空格和标点符号隔单词 while (token != NULL) { for (int i = 0; i < strlen(token); i++) { token[i] = tolower(token[i]); // 统一转为小写字母 } insertOrUpdate(token); token = strtok(NULL, " ,.-"); } // 输出词频 printFreq(); return 0; } ``` 这样,输入一段文本,程序会输出每个单词的词频。注意,此代码只是一个简单示例,没有考虑一些特殊情况,如单词超长等,需根据实际需求进行调整和完善。 ### 回答2: 题目要求使用C语言编写一个程序,统计一个给定字符串中各个单词出现的次数。下面是一个简单的C语言代码示例: ```c #include <stdio.h> #include <string.h> void wordFrequency(char *str) { int len = strlen(str); int count = 1; for (int i = 0; i < len; i++) { if (str[i] == ' ') { count++; continue; } while (str[i] != ' ' && i < len) { i++; } } printf("单词个数:%d\n", count); } int main() { char str[100]; printf("请输入字符串:"); gets(str); wordFrequency(str); return 0; } ``` 代码中的wordFrequency函数用于统计单词个数。它首先通过strlen函数获取字符串的长度,然后使用一个循环遍历字符串。在循环中,每次遇到空格符就将计数器加一,忽略其他符号。最后输出计数器的值,即为单词个数。 在主函数中,我们使用gets函数获取用户输入的字符串,并调用wordFrequency函数进行统计。最后输出结果。注意,由于使用了gets函数,输入的字符串长度不能超过100个字符。 这是一个简单的单词频率统计程序,只能统计单词的个数,并不考虑重复出现的情况。如果需要统计每个单词的具体出现次数,需要对代码进行进一步的修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值