数据结构PTA 进阶实验5-3.2 新浪微博热门话题(分离链接法 )

进阶实验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. 哈希表的链表结点LNode类型。LNode类型中应该包括:存储话题的字符串类型、此话题对应的微博条数int类型WBN、此时微博的编号WBL、还有指向下一个结点的指针。
  5. 注意此题陷阱很多,比如:对于@这种符号,需要把它当作空格处理,每个存储的字符串都需要保证首字母大写,其余部分由字母,空格,数字三种类型组成。

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

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

#define MAXCHAR 140
//#define NULL -1

typedef char EleType[MAXCHAR+1];

typedef struct LNode *PtrToLNode;
struct LNode
{
    EleType Data;
    int WBL;
    int WBN;
    PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

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

HashTable CreateTable(int TableSize)
{
    HashTable H = (HashTable)malloc(sizeof(struct TblNode));
    H->TableSize = 2*TableSize;
    H->Heads = (List)malloc(H->TableSize*sizeof(struct LNode));

    for(int i=0; i<H->TableSize; i++)
    {
        H->Heads[i].Data[0] = '\0';
        H->Heads[i].Next = NULL;
        H->Heads[i].WBN = 0;
        H->Heads[i].WBL = 0;
    }

    return H;
}

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

Position Find(HashTable H, EleType key)
{
    int Pos = Hash(H, key);
    Position P = H->Heads[Pos].Next;
    while(P && strcmp(P->Data, key))
        P = P->Next;

    return P;
}

void Insert(HashTable H, EleType key, int i)
{
    Position P, NewCell;
    int Pos;

    P = Find(H, key);
    if(P == NULL)
    {
        NewCell = (Position)malloc(sizeof(struct LNode));
        strcpy(NewCell->Data, key);
        NewCell->WBN = 1;
        NewCell->WBL = i;

        Pos = Hash(H, key);
        NewCell->Next = H->Heads[Pos].Next;
        H->Heads[Pos].Next = NewCell;
    }
    else if(P->WBL != i)
    {
        P->WBN++;
        P->WBL = i;
    }

}


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 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;

    Position P;

    for(i=0; i<H->TableSize; i++)
    {
        if(H->Heads[i].Next != NULL)
        {
            P = H->Heads[i].Next;
            while(P)
            {
                if(P->WBN > MaxNumber)
                {
                    MaxNumber = P->WBN;
                    MaxCount = 1;
                    strcpy(output, P->Data);
                }
                else if(P->WBN == MaxNumber)
                {
                    MaxCount++;
                    if(strcmp(output, P->Data)>0)
                        strcpy(output, P->Data);
                }
                P = P->Next;
            }
        }
    }

    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;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值