5-46 新浪微博热门话题   (30分)

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

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

输入格式:

输入说明:输入首先给出一个正整数NN(\le 10^5≤10​5​​),随后NN行,每行给出一条英文微博,其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题,如果长度超过40个字符,则只保留前40个字符。输入保证#成对出现。

输出格式:

第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出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 ... 这题对字符串处理要求比较多,在字符串比较的时候要遵守一定规则(字母和数字相同即相同),但是在输出时却要原样输出,而且同一行中一个话题不可以加入两次,这样没办法使用cstring里面的函数
比较尴尬,通过这题学了一个分离字符串的函数strtok,这个函数和python里面得split函数差不多,都是把一个字符串分隔成以规定字符间隔得多个字符串,下面附上第一次做的源码:这次没有考虑
到比较规则。。这题我搜了一下网上也没有答案,对于题意我还有一点疑问,就是当两个按规则比较相等的字符串为出现次数最多的热门话题时,也输出字典序小的那个吗??
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
#define MAXN 10005
typedef long long LL;
/* */ typedef struct node
{
char id[];
int cnt;
int line;
struct node* next;
}*List;
typedef struct tb
{
int Tablesize;
List *list;
}*Hashlist;
LL Hash(char key[],LL size)
{
LL tmp = ;
for(LL i=;i<;i++)
{
if( !( (key[i]<='z'&&key[i]>='a')||(key[i]<=''&&key[i]>='') ))
continue;
if(key[i]=='x')
tmp = (tmp*+)%size;
else
tmp = (tmp* + key[i]-'')%size;
}
if(tmp>=)
return tmp;
else
return (tmp+size)%size;
}
int NextPrime(int x)
{
int i;
for (int Next = x; ; Next++)
{
for (i = ; i * i <= Next; i++)
if (Next % i == )
break;
if (i * i > Next)
return Next;
}
}
Hashlist Init(int size)
{
Hashlist H = (Hashlist)malloc(sizeof(tb));
H->Tablesize = NextPrime(size);
H->list = (List*)malloc(sizeof(List)*H->Tablesize);
for(int i=;i< H->Tablesize;i++)
{
H->list[i] =(List)malloc(sizeof(node));
H->list[i]->next = NULL;
H->list[i]->cnt = ;
H->list[i]->line = -;
}
return H;
}
List Find(char key[],Hashlist H)
{
List t = H->list[Hash(key,H->Tablesize)];
List p = t->next;
while(p!=NULL && strcmp(key,p->id))
p = p->next;
return p;
}
void Insert(char key[],Hashlist H,int line)
{
int len = strlen(key);
for(int i=;i<len;i++)
key[i] = tolower(key[i]);
//cout<<key<<endl;
List t = H->list[Hash(key,H->Tablesize)];
List f = Find(key,H);
if(f==NULL)
{
List tmp = (List)malloc(sizeof(node));
tmp->cnt = ;
tmp->line = line;
strcpy(tmp->id,key);
tmp->next = t->next;
t->next = tmp;
}
else
{
if((f->line)!=line)
(f->cnt)++;
}
}
void Findmax(Hashlist H)
{
int max = -,same = ;
char ans[];
for(int i=;i< H->Tablesize;i++)
{
List t = H->list[i];
List p = t->next;
while(p!=NULL)
{
if(p->cnt>max)
{
max = p->cnt;
same = ;
strcpy(ans,p->id);
}
else if(p->cnt==max)
{
if(strcmp(ans,p->id)>)
strcpy(ans,p->id);
same++;
}
p = p->next;
}
}
if(ans[]<='z'&&ans[]>='a')
ans[] = toupper(ans[]);
printf("%s\n%d\n",ans,max);
if(same>)
printf("And %d more ...\n",same-);
}
int main()
{
int n;
char str[];
scanf("%d",&n);
Hashlist H = Init(n);
getchar();
for(int l=;l<=n;l++)
{
gets(str);
char * p;
p = strtok(str,"#");
int cnt = ;
while(p!=NULL)
{
if(cnt%==)
Insert(p,H,l);
cnt++;
p = strtok(NULL,"#");
}
}
Findmax(H);
return ;
}

5-46 新浪微博热门话题 (30分)——unfinished HASH的更多相关文章

  1. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  2. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  3. PTA 11-散列4 Hard Version (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version   (30分) Given ...

  4. 用python+selenium抓取微博24小时热门话题的前15个并保存到txt中

    抓取微博24小时热门话题的前15个,抓取的内容请保存至txt文件中,需要抓取排行.话题和阅读数 #coding=utf-8 from selenium import webdriver import ...

  5. PTA 07-图5 Saving James Bond - Hard Version (30分)

    07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie ...

  6. PTA 社交网络图中结点的“重要性”计算(30 分)

    7-12 社交网络图中结点的“重要性”计算(30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互 ...

  7. L3-015 球队“食物链” (30 分)

    L3-015 球队“食物链” (30 分)   某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席 ...

  8. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  9. PTA 7-2 二叉搜索树的结构(30 分)

    7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...

随机推荐

  1. $CF241D\ Numbers$

    problem 题目大意: 给你n个数和p,都小于50000要求留下若干个数字,使得剩下的数字异或为0,并且从左到右串联起来可以被p整除,求一种这样的方案. 搜索 #include <bits/ ...

  2. MongoDB Built-In Roles(内置角色)

    1. 数据库用户角色:read.readWrite; 2. 数据库管理角色:dbAdmin.dbOwner.userAdmin: 3. 集群管理角色:clusterAdmin.clusterManag ...

  3. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  4. magento “Model collection resource name is not defined” 错误

    问题出现于使用Grid时,解决方案.在使用的Model处添加 public function _construct() { parent::_construct(); $this->_init( ...

  5. P3373 【模板】线段树 2 区间求和 区间乘 区间加

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含三个整数N.M.P,分别 ...

  6. http请求和响应头部

    说一说常见的请求头和响应头都有什么呢? 1)请求(客户端->服务端[request])     GET(请求的方式) /newcoder/hello.html(请求的目标资源) HTTP/1.1 ...

  7. jboss项目设置域名

    1.在jboss-web.xml中添加<virtual-host>www.ceshi.com</virtual-host> <jboss-web> <cont ...

  8. 北大ACM(POJ1015-Jury Compromise)

    Question:http://poj.org/problem?id=1015 问题点:DP. Memory: 1352K Time: 94MS Language: C++ Result: Accep ...

  9. Ubuntu-Python2.7安装 scipy,numpy,matplotlib

    sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...

  10. URL解析-URLComponents

    let components = URLComponents(url: fakeUrl, resolvingAgainstBaseURL: false)! http://10.100.140.84/m ...