您的位置:首页 > 理论基础 > 数据结构算法

数据结构PTA 进阶实验5-3.2 新浪微博热门话题

2020-07-19 16:23 190 查看

进阶实验5-3.2 新浪微博热门话题

题目

新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。

本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。

输入格式:

输入说明:输入首先给出一个正整数N(≤105),随后N行,每行给出一条英文微博,其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题,输入保证#成对出现。

输出格式:

第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出And k more …,其中k是另外几条热门话题的条数。输入保证至少存在一条话题。

注意:两条话题被认为是相同的,如果在去掉所有非英文字母和数字的符号、并忽略大小写区别后,它们是相同的字符串;同时它们有完全相同的分词。输出时除首字母大写外,只保留小写英文字母和数字,并用一个空格分隔原文中的单词。

输入样例:

4
This is a #test of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic

输出样例:

Hot
2
And 1 more …

解法

思路

  1. 读取话题。这个可以用while(p != ‘#’)来实现,
  2. 读取微博条。这个可以用while(p != ‘\n’)来实现,也可以用if break组合实现。
  3. 关于两条话题相同与否的判定。首先限制存储的字符ASCII码的范围在’A’ ~ ‘Z’或’a’ ~ 'z’的范围。然后在进行字符串的比较时,应该逐位比较,如果他们之间每位的差值为0或32(‘a’ - ‘A’)或-32(‘A’ - ‘a’),那么这就是一样的话题。
  4. 哈希表的Cell类型。Cell类型中应该包括:存储话题的字符串类型、此话题对应的微博条数int类型、此位置是否为空的EntryType类型(用enum方式定义)、此时微博的编号Collected.
  5. 注意此题陷阱很多,比如:对于@这种符号,需要把它当作空格处理,每个存储的字符串都需要保证首字母大写,其余部分由字母,空格,数字三种类型组成。

实现

对于本代码,不知道为何在第四步检测中会出现运行超时的情况(本代码的读入操作时逐个字符读入,在输出的时候,首先用了一个循环扫描整个数组,找到出现条数的最大值MaxNumber,接着再扫描一遍数组,找到这个MaxNumber对应的最小字符串(通过strcmp比较),最后输出。这种方法按理来说时间复杂度不算大吧…欢迎评论区中指正),这个后续更新了再解决。下面的实现能够通过测试一到三。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXCHAR 140

typedef enum{empty, exist} EntryType;
typedef char EleType[MAXCHAR+1];
typedef struct HashEntry Cell;
struct HashEntry
{
EleType Data;
int Number;
EntryType Info;
int Collected;
};

typedef struct TblNode *HashTable;
struct TblNode
{
int TableSize;
Cell *Cells;
};

HashTable CreateTable(int TableSize)
{
HashTable H = (HashTable)malloc(sizeof(struct TblNode));
H->TableSize = 4*TableSize;
H->Cells = (Cell*)malloc(H->TableSize*sizeof(struct HashEntry));

for(int i=0; i<H->TableSize; i++)
{
H->Cells[i].Info = empty;
H->Cells[i].Number = 0;
H->Cells[i].Collected = 0;
}
return H;
}

int Hash(HashTable H, EleType key)
{
int hkey,count,ans;
hkey = count = 0;
while(key[count]!='\0' && count < 5)
{
hkey = hkey*3 + key[count];
count++;
}
return hkey % H->TableSize;
}

int CompareIgnoreCap(EleType a, EleType b)
{
int count = 0;
int flag = 1;
while(a[count] != '\0' && b[count] != '\0')
{
if( (a[count]-b[count]) != 0  &&  (a[count]-b[count]) != 'A'-'a' &&  (a[count]-b[count]) != 'a'-'A')
{
flag = 0;
break;
}
count++;
}
if(a[count] != '\0' || b[count] != '\0')
flag = 0;
return flag;

}

int LinearFind(HashTable H, EleType key)
{
int CurPos, NewPos;
int count = 0;
CurPos = NewPos = Hash(H, key);
while(H->Cells[NewPos].Info != empty && !CompareIgnoreCap(H->Cells[NewPos].Data, key))
{
count++;
NewPos = CurPos + count;
if(NewPos >= H->TableSize)
NewPos = NewPos%H->TableSize;
}
return NewPos;
}

void Insert(HashTable H, EleType key, int i)
{
int Pos = LinearFind(H, key);
if(H->Cells[Pos].Info == empty)
{
strcpy(H->Cells[Pos].Data, key);
H->Cells[Pos].Number = 1;
H->Cells[Pos].Info = exist;
H->Cells[Pos].Collected = i;
}
else if(H->Cells[Pos].Collected != i)
{
H->Cells[Pos].Number++;
H->Cells[Pos].Collected = i;
}

}

int IsALetter(char tmp)
{
int flag = 0;
if(tmp>='A' && tmp<='Z')
flag = 1;
if(tmp>='a' && tmp<='z')
flag = 2;
if(tmp == ' ')
flag = 3;
if(tmp>='0' && tmp<='9')
flag = 4;
if(tmp == '@')
flag = 5;
return flag;
}

char BToS(char X)
{
return X-'A'+'a';
}

char SToB(char X)
{
return X-'a'+'A';
}

void ReadTopics(HashTable H, int N)
{
int i, Pos,countT;
char tmp = 'k'; //select randomly
EleType key;
int count,tag;
for(i=0; i<N+1; i++)
{
scanf("%c", &tmp);
countT = 0;
while(tmp != '\n' && countT<1000)
{
if(tmp == '#')
{
count = 0;
scanf("%c", &tmp);
while(tmp != '#')
{
tag = IsALetter(tmp);
if(tag)
{
if(count == 0 && tag == 2)
tmp = SToB(tmp);
if(count > 0 && tag == 1)
tmp = BToS(tmp);
if(count > 0 && tag == 5)
tmp = ' ';
key[count++] = tmp;
}

scanf("%c", &tmp);
}
key[count] = '\0';
//Insert key

Insert(H, key, i);
}
scanf("%c", &tmp);
countT++;
}
}

}

void PrintResult(HashTable H)
{
int i,MaxNumber, MaxCount;
EleType output;
MaxCount = MaxNumber = 0;
for(i=0; i<H->TableSize; i++)
{
if(H->Cells[i].Number > MaxNumber)
MaxNumber = H->Cells[i].Number;
}

for(i=0; i<H->TableSize; i++)
{
if(H->Cells[i].Number == MaxNumber)
{
MaxCount++;
if(MaxCount == 1)
strcpy(output, H->Cells[i].Data);
else if(strcmp(output, H->Cells[i].Data)>0)
strcpy(output, H->Cells[i].Data);
}
}
printf("%s\n", output);
printf("%d\n", MaxNumber);
if(MaxCount-1>0)
printf("And %d more ...", MaxCount-1);
}

int main()
{

int N;
scanf("%d", &N);
HashTable H = CreateTable(N);

ReadTopics(H, N);
PrintResult(H);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: