您的位置:首页 > 编程语言

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分代码,只有一个地方和上面不同,可以自行比较:

#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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: