PTA 7-18(查找) 新浪微博热门话题(30 分) 30分代码 (已更新)
2017-10-11 10:53
1251 查看
前段时间做的题,当时没完全通过,今早上想到一种情况,试了一下通过了,题目没问题,就是解释的不够清楚
题中说的那个同样的分词算作一个包括 : hello world = hello+world = hello world 这种情况,,,
方法就是 map 映射
(注:博客作为交流使用,切勿抄袭应付作业)
这是完全通过的代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000+7, INF = 0x7f7f7f7f;
int n;
char s[147];
string t;
set<string> ans, cur;
map<string, int> mp;
struct node {
string name;
int cnt;
}a[maxn];
bool cmp(node a, node b) {
if(a.cnt == b.cnt) return a.name < b.name;
return a.cnt > b.cnt;
}
void work(int len) {
cur.clear();
for(int i = 0; i < len; ++i) {
if(s[i] == '#') {
int j = i+1;
int j_ = j;
while(s[j_] != '#') j_++;
t.clear();
for(; j <= j_ ; ++j) {
if(s[j] == '#') {
cur.insert(t);
int k = t.size() - 1;
while(t[k] == ' ') { t.erase(k, 1); k--; }
break;
}
if(s[j] >= 'A' && s[j] <= 'Z') {
t.push_back(s[j]-'A'+'a');
}
else if(isalpha(s[j]) || s[j] == ' ' || isdigit(s[j])) {
t.push_back(s[j]);
}
else if (isalpha(s[j+1])) {
t.push_back(' ');
}
}
i = j_;
}
}
for(set<string>::iterator it = cur.begin(); it != cur.end(); ++it) {
mp[*it]++;
ans.insert(*it);
}
}
void solve() {
int i = 0;
for(set<string>::iterator it = ans.begin(); it != ans.end(); ++it) {
a[i].name = *it;
a[i].cnt = mp[*it];
i++;
}
sort(a, a+i, cmp);
string ans1 = a[0].name;
int ans2 = a[0].cnt, ans3 = 0;
for(int j = 1; j < i; ++j) {
if(a[j].cnt == a[j-1].cnt) ans3++;
else break;
}
printf("%c", ans1[0]-'a'+'A');
for(int i = 1; i < ans1.size() ; ++i) {
if(ans1[i] == ' ' && ans1[i-1] == ' ') continue;
printf("%c", ans1[i]);
}
puts("");
printf("%d\n", ans2);
if(ans3) printf("And %d more ...\n", ans3);
}
int main() {
scanf("%d", &n); getchar();
ans.clear();
for(int i = 0; i < n; ++i) {
gets(s);
int len = strlen(s);
work(len);
}
solve();
return 0;
}
/*
4
This is a #te st of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic
*/
这是 不完全通过的 24分代码,只有一个地方和上面不同,可以自行比较:
题中说的那个同样的分词算作一个包括 : hello world = hello+world = hello world 这种情况,,,
方法就是 map 映射
(注:博客作为交流使用,切勿抄袭应付作业)
这是完全通过的代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100000+7, INF = 0x7f7f7f7f;
int n;
char s[147];
string t;
set<string> ans, cur;
map<string, int> mp;
struct node {
string name;
int cnt;
}a[maxn];
bool cmp(node a, node b) {
if(a.cnt == b.cnt) return a.name < b.name;
return a.cnt > b.cnt;
}
void work(int len) {
cur.clear();
for(int i = 0; i < len; ++i) {
if(s[i] == '#') {
int j = i+1;
int j_ = j;
while(s[j_] != '#') j_++;
t.clear();
for(; j <= j_ ; ++j) {
if(s[j] == '#') {
cur.insert(t);
int k = t.size() - 1;
while(t[k] == ' ') { t.erase(k, 1); k--; }
break;
}
if(s[j] >= 'A' && s[j] <= 'Z') {
t.push_back(s[j]-'A'+'a');
}
else if(isalpha(s[j]) || s[j] == ' ' || isdigit(s[j])) {
t.push_back(s[j]);
}
else if (isalpha(s[j+1])) {
t.push_back(' ');
}
}
i = j_;
}
}
for(set<string>::iterator it = cur.begin(); it != cur.end(); ++it) {
mp[*it]++;
ans.insert(*it);
}
}
void solve() {
int i = 0;
for(set<string>::iterator it = ans.begin(); it != ans.end(); ++it) {
a[i].name = *it;
a[i].cnt = mp[*it];
i++;
}
sort(a, a+i, cmp);
string ans1 = a[0].name;
int ans2 = a[0].cnt, ans3 = 0;
for(int j = 1; j < i; ++j) {
if(a[j].cnt == a[j-1].cnt) ans3++;
else break;
}
printf("%c", ans1[0]-'a'+'A');
for(int i = 1; i < ans1.size() ; ++i) {
if(ans1[i] == ' ' && ans1[i-1] == ' ') continue;
printf("%c", ans1[i]);
}
puts("");
printf("%d\n", ans2);
if(ans3) printf("And %d more ...\n", ans3);
}
int main() {
scanf("%d", &n); getchar();
ans.clear();
for(int i = 0; i < n; ++i) {
gets(s);
int len = strlen(s);
work(len);
}
solve();
return 0;
}
/*
4
This is a #te st of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic
*/
这是 不完全通过的 24分代码,只有一个地方和上面不同,可以自行比较:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 100000+7, INF = 0x7f7f7f7f; int n; char s[147]; string t; set<string> ans, cur; map<string, int> mp; struct node { string name; int cnt; }a[maxn]; bool cmp(node a, node b) { if(a.cnt == b.cnt) return a.name < b.name; return a.cnt > b.cnt; } void work(int len) { cur.clear(); for(int i = 0; i < len; ++i) { if(s[i] == '#') { int j = i+1; int j_ = j; while(s[j_] != '#') j_++; //while(s[j] == ' ') j++; t.clear(); for(; j <= j_ ; ++j) { if(s[j] == '#') { cur.insert(t); int k = t.size() - 1; while(t[k] == ' ') { t.erase(k, 1); k--; } //cout << t << "==== " << endl; break; } if(s[j] >= 'A' && s[j] <= 'Z') { t.push_back(s[j]-'A'+'a'); } else if(isalpha(s[j]) || s[j] == ' ' || isdigit(s[j])) { t.push_back(s[j]); } } i = j_; } } for(set<string>::iterator it = cur.begin(); it != cur.end(); ++it) { mp[*it]++; ans.insert(*it); } } void solve() { int i = 0; for(set<string>::iterator it = ans.begin(); it != ans.end(); ++it) { a[i].name = *it; a[i].cnt = mp[*it]; i++; } sort(a, a+i, cmp); string ans1 = a[0].name; int ans2 = a[0].cnt, ans3 = 0; for(int j = 1; j < i; ++j) { if(a[j].cnt == a[j-1].cnt) ans3++; else break; } printf("%c", ans1[0]-'a'+'A'); for(int i = 1; i < ans1.size() ; ++i) { if(ans1[i] == ' ' && ans1[i-1] == ' ') continue; printf("%c", ans1[i]); } puts(""); printf("%d\n", ans2); if(ans3) printf("And %d more ...\n", ans3); } int main() { scanf("%d", &n); getchar(); ans.clear(); for(int i = 0; i < n; ++i) { gets(s); //puts(s); int len = strlen(s); //cout << len << " ++++ " << endl; work(len); } solve(); return 0; } /* 4 This is a #te st of topic#. Another #Test of topic.# This is a #Hot# #Hot# topic Another #hot!# #Hot# topic */
相关文章推荐
- PTA 7-18 新浪微博热门话题
- 5-46 新浪微博热门话题 (30分)——unfinished HASH
- PTA 7-9(树) 目录树(30 分) 30分代码 建树+输出
- 5-46 新浪微博热门话题 (30分)
- PTA 7-6(队列) 银行排队问题之单队列多窗口加VIP服务(30 分) 30分代码 大模拟
- PTA 7-4(队列) 银行排队问题之单窗口“夹塞”版(30 分) 30分代码
- PTA 7-12(图) 社交网络图中结点的“重要性”计算(30 分) 30分代码
- PTA 7-15(查找) 航空公司VIP客户查询(25 分) 25分代码 哈希方法
- Python 获取新浪微博的热门话题 (API)
- hiho 18 19 使用线段树进行区间查找和更新
- PTA 7-6(队列) 银行排队问题之单队列多窗口加VIP服务(30 分) 30分代码
- 如何查找博客热门话题
- [前端代码] 3月2日更新-近期200+热门微信小程序demo源码下载汇总
- 新浪微博数据挖掘(python)本周人们在讨论的热门话题的提取
- PTA 7-17(查找) 字符串关键字的散列映射(25 分) 25分代码
- 新浪微博热门话题
- 热门话题:使用WCF实现SOA面向服务编程—— 架构设计(附原代码)
- Hibernate对数据库删除、查找、更新操作实例代码
- 《数据结构学习与实验指导》5-7:新浪微博热门话题
- [置顶] 【python 新浪微博爬虫】python 爬取新浪微博24小时热门话题top500