新浪微博熱門話題(30 分)
新浪微博可以在發言中嵌入“話題”,即將發言中的話題文字寫在一對“#”之間,就可以生成話題鏈接,點擊鏈接可以看到有多少人在跟自己討論相同或者相似的話題。新浪微博還會隨時更新熱門話題列表,並將最熱門的話題放在醒目的位置推薦大家關注。
本題目要求實現一個簡化的熱門話題推薦功能,從大量英文(因為中文分詞處理比較麻煩)微博中解析出話題,找出被最多條微博提到的話題。
輸入格式:
輸入說明:輸入首先給出一個正整數N(≤105),隨后N行,每行給出一條英文微博,其長度不超過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函數差不多,都是把一個字符串分隔成以規定字符間隔得多個字符串,下面附上第一次做的源碼:這次沒有考慮
到比較規則。。這題我搜了一下網上也沒有答案,對於題意我還有一點疑問,就是當兩個按規則比較相等的字符串為出現次數最多的熱門話題時,也輸出字典序小的那個嗎??
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<string> 6 using namespace std; 7 #define MAXN 10005 8 typedef long long LL; 9 /* 10 11 */ 12 13 typedef struct node 14 { 15 char id[41]; 16 int cnt; 17 int line; 18 struct node* next; 19 }*List; 20 typedef struct tb 21 { 22 int Tablesize; 23 List *list; 24 }*Hashlist; 25 LL Hash(char key[],LL size) 26 { 27 LL tmp = 0; 28 for(LL i=13;i<18;i++) 29 { 30 if( !( (key[i]<='z'&&key[i]>='a')||(key[i]<='9'&&key[i]>='0') )) 31 continue; 32 if(key[i]=='x') 33 tmp = (tmp*10+10)%size; 34 else 35 tmp = (tmp*10 + key[i]-'0')%size; 36 } 37 if(tmp>=0) 38 return tmp; 39 else 40 return (tmp+size)%size; 41 } 42 int NextPrime(int x) 43 { 44 int i; 45 for (int Next = x; ; Next++) 46 { 47 for (i = 2; i * i <= Next; i++) 48 if (Next % i == 0) 49 break; 50 if (i * i > Next) 51 return Next; 52 } 53 } 54 Hashlist Init(int size) 55 { 56 Hashlist H = (Hashlist)malloc(sizeof(tb)); 57 H->Tablesize = NextPrime(size); 58 H->list = (List*)malloc(sizeof(List)*H->Tablesize); 59 for(int i=0;i< H->Tablesize;i++) 60 { 61 H->list[i] =(List)malloc(sizeof(node)); 62 H->list[i]->next = NULL; 63 H->list[i]->cnt = 0; 64 H->list[i]->line = -1; 65 } 66 return H; 67 } 68 List Find(char key[],Hashlist H) 69 { 70 List t = H->list[Hash(key,H->Tablesize)]; 71 List p = t->next; 72 while(p!=NULL && strcmp(key,p->id)) 73 p = p->next; 74 return p; 75 } 76 void Insert(char key[],Hashlist H,int line) 77 { 78 int len = strlen(key); 79 for(int i=0;i<len;i++) 80 key[i] = tolower(key[i]); 81 //cout<<key<<endl; 82 List t = H->list[Hash(key,H->Tablesize)]; 83 List f = Find(key,H); 84 if(f==NULL) 85 { 86 List tmp = (List)malloc(sizeof(node)); 87 tmp->cnt = 1; 88 tmp->line = line; 89 strcpy(tmp->id,key); 90 tmp->next = t->next; 91 t->next = tmp; 92 } 93 else 94 { 95 if((f->line)!=line) 96 (f->cnt)++; 97 } 98 } 99 void Findmax(Hashlist H) 100 { 101 int max = -1,same = 1; 102 char ans[41]; 103 for(int i=0;i< H->Tablesize;i++) 104 { 105 List t = H->list[i]; 106 List p = t->next; 107 while(p!=NULL) 108 { 109 if(p->cnt>max) 110 { 111 max = p->cnt; 112 same = 1; 113 strcpy(ans,p->id); 114 } 115 else if(p->cnt==max) 116 { 117 if(strcmp(ans,p->id)>0) 118 strcpy(ans,p->id); 119 same++; 120 } 121 p = p->next; 122 } 123 } 124 if(ans[0]<='z'&&ans[0]>='a') 125 ans[0] = toupper(ans[0]); 126 printf("%s\n%d\n",ans,max); 127 if(same>1) 128 printf("And %d more ...\n",same-1); 129 } 130 int main() 131 { 132 int n; 133 char str[141]; 134 scanf("%d",&n); 135 Hashlist H = Init(n); 136 getchar(); 137 for(int l=1;l<=n;l++) 138 { 139 gets(str); 140 char * p; 141 p = strtok(str,"#"); 142 int cnt = 1; 143 while(p!=NULL) 144 { 145 if(cnt%2==0) 146 Insert(p,H,l); 147 cnt++; 148 p = strtok(NULL,"#"); 149 } 150 } 151 Findmax(H); 152 return 0; 153 }
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <string> 6 #include <map> 7 using namespace std; 8 map<string, int> vis; 9 map<string, int> cnt; 10 map<string, int>::iterator it; 11 bool judge(char c) { 12 if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || '0' <= c && c <= '9' || c == ' ') return true; 13 return false; 14 } 15 int main() { 16 int n; scanf("%d", &n); getchar(); 17 char str[1000]; 18 string s; 19 for (int i = 1; i <= n; i++) { 20 gets(str); 21 bool flag = false; 22 vis.clear(); 23 for (int i = 0; str[i]; i++) { 24 if (str[i] == '#' && flag == false) { 25 s.clear(); flag = true; 26 } 27 else if (str[i] != '#' && flag == true) { 28 if (!judge(str[i])) str[i] = ' '; 29 if ('A' <= str[i] && str[i] <= 'Z') str[i] += 32; 30 int len = s.size(); 31 if (len > 0 && str[i] == ' ' && s[len - 1] == ' ') continue; 32 s.push_back(str[i]); 33 } 34 else if (str[i] == '#' && flag == true) { 35 string ss; 36 if (s[0] == ' ') for (int i = 1; i < s.size(); i++) ss.push_back(s[i]); 37 else if (s[s.size() - 1] == ' ') for (int i = 0; i < s.size() - 1; i++) ss.push_back(s[i]); 38 else ss = s; 39 //cout << ss << endl; 40 if (!vis[ss]) { 41 vis[ss] = 1; 42 cnt[ss]++; 43 } 44 flag = false; 45 } 46 } 47 } 48 int num = 0; 49 int res = 0; 50 string ans; 51 for (it = cnt.begin(); it != cnt.end(); it++) { 52 string x = it->first; int y = it->second; 53 if (y > res) { 54 num = 0; 55 res = y; 56 ans = x; 57 } 58 else if (y == res) num++; 59 } 60 ans[0] -= 32; 61 cout << ans << endl << res << endl; 62 if (num) cout << "And " << num << " more ..." << endl; 63 }
