输入为一行,由若干个单词和句号组成
输出格式参见样例。
A blockhouse is a small castle that has four openings through which to shoot.
a:2 blockhouse:1 castle:1 four:1 has:1 is:1 openings:1 shoot:1 small:1 that:1 through:1 to:1 which:1
测试样例简直有毒。。4x4)和(at都算单词#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> typedef struct str0 { char ch[1000]; int count; int flag; } str1;str1 str[1000]; str1 temp; int main() {int i = 0; while(scanf("%s", str[i].ch) != EOF) { i++; } int sum = i; int j; for(i = 0; i < sum; i++) { str[i].flag = 1; //一开始都不是克隆的 } for(i = 0; i < sum; i++) { int len = strlen(str[i].ch); for(j = 0; j < len; j++) { if(str[i].ch[j] >= 'A' && str[i].ch[j] <= 'Z') { str[i].ch[j] = str[i].ch[j] - 'A' + 'a'; //变成小写字母 } else if(str[i].ch[j] < 'a' || str[i].ch[j] > 'z') { //不是小写字母 //删除这个字符 if((str[i].ch[j]=='.'||str[i].ch[j]==',')&&j==len-1) str[i].ch[j] = '\0'; } } }for(i = 0; i < sum; i++) { str[i].count = 1; if(str[i].flag == 1) { //这个不是克隆的,就可以记数了 for(j = i + 1 ; j < sum; j++) { if(strcmp(str[i].ch, str[j].ch) == 0) { str[i].count++; str[j].flag = 0; //这个是克隆的! } } } } //记数都完成了,现在要字典序输出! //冒泡排序 for(i = 0; i < sum - 1; i++) { for(j = 0; j < sum - 1 - i; j++) { if(strcmp(str[j].ch, str[j + 1].ch) > 0) { //前面的字符串更大 //交换 temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp; } } } //现在开始输出 for(i = 0; i < sum; i++) { if(str[i].flag == 1) printf("%s:%d\n", str[i].ch, str[i].count); } return 0; }
#include<bits/stdc++.h> using namespace std; void Trains(string& str) { for(int i = 0;i < str.length(); i++) { if(isupper(str[i])) str[i] = tolower(str[i]); if(str[i] == ',' || str[i] == '.') str[i] = ' '; } } int main() { ios::sync_with_stdio(false); string str; while(getline(cin, str)) { map<string, int> m; Trains(str); stringstream ss(str); string word; while(ss >> word) m[word]++; for(pair<string, int> x : m) cout << x.first << ":" << x.second << "\n"; } return 0; }
#include<iostream> (720)#include<string> #include<regex> (1081)#include<vector> #include<map> using namespace std; struct datas { string s; int count; bool operator<(datas c) { //if (count == c.count) return s < c.s; //else return count > c.count; } }; int main() { string s; //getline(cin, s, '.'); while (getline(cin, s)) { vector<datas> contain; map<string, int> sg; regex pattern("([)(a-zA-Z0-9]+)"); //匹配字符串 smatch result; string::const_iterator str = s.begin(); string::const_iterator str_end = s.end(); while (regex_search(str, str_end, result, pattern)) { string a = result[0]; a[0] = tolower(a[0]); sg[a]++; str = result[0].second; //无括号 } map<string, int>::iterator it; for (it = sg.begin(); it != sg.end(); it++) { string k = it->first; //k[0] = tolower(k[0]); datas a; a.s = k; a.count = it->second; contain.push_back(a); } sort(contain.begin(), contain.end()); for (int i = 0; i < contain.size(); i++) { cout << contain[i].s << ":" << contain[i].count << endl; } } }
#include<iostream> #include<map> #include<string> #include<vector> #include<algorithm> using namespace std; void fun(pair<string,int> p) { cout << p.first << ":" << p.second << endl; } bool cmp(pair<string,int> p1,pair<string,int> p2) { return p1.second > p2.second; } int main() { string t; map<string,int> mp; pair<map<string,int>::iterator,int> pr; while(cin >> t){ if(t[t.length()-1] == ',' || t[t.length()-1] == '.') t = t.substr(0,t.length()-1); // 左闭右开的区间 for(int i = 0; i < t.length(); i++) t[i] = tolower(t[i]); pr = mp.insert(pair<string,int>(t,1)); if(!pr.second) //插入失败 pr.first->second++; // mp[t]++; // 看大佬代码发现,以上三行可以用这一句直接代替 } // vector<pair<string,int> > v(mp.begin(),mp.end()); //map to vector // stable_sort(v.begin(),v.end(),cmp); // 个人认为排序需要稳定 for_each(v.begin(),v.end(),fun); // for_each(mp.begin(),mp.end(),fun); }
有人能告知哪里出错了吗?
import re sentence = input() sentence = sentence.lower() #转大写用upper() pattern = r'[,.\s]' s_split = re.split(pattern, sentence) s_none = list(filter(None, s_split)) result = {} for i in s_none: result[i] = s_none.count(i) for k in sorted(result): temp = str(k) + ':' + str(result[k]) print(temp)
//这道题的答案是按照字典序排列的,只要将map中的元素顺序输出即可。 #include <bits/stdc++.h> using namespace std; int main() { string s; while(getline(cin, s)) { map<string, int> mp; string temp; for(int i = 0; i < s.size(); i++) { if(s[i] == ' ' || s[i] == ',' || s[i] == '.') { if(temp != "") mp[temp]++; temp = ""; } else { temp += tolower(s[i]); } } for(auto it = mp.begin(); it != mp.end(); it++) { cout << it->first << ":" << it->second << endl; } } return 0; }
#include<iostream> #include<string> #include<map> #include<vector> using namespace std; int main() { string str; string tmp; vector<string>arr; getline(cin,str); map<string,int> mp; multimap<int,string,greater<>> mp2; for(int i = 0;i<str.size();++i) { //统一小写; if(str[i]<='Z'&&str[i]>='A') { str[i]+='a'-'A'; } if(str[i]!=' '&&str[i]!='.') { tmp+=str[i]; } else { arr.push_back(tmp); tmp.clear(); } } for(auto& e:arr) { mp[e]++; } for(auto& e:mp) { mp2.insert(make_pair(e.second,e.first)); } for(auto& e:mp2) { cout<<e.second<<':'<<e.first<<endl; } return 0; } 先过一遍map保证字节序并统计次数,再利用mutimap底层红黑树按照插入次序保持不变(在key相等的情况下)遍历插入即可
#include <iostream> #include <unordered_map> #include <utility> #include <string> #include <queue> #include <unordered_map> #include <vector> using namespace std; int main() { // 仿函数比较,建大堆,返回小于的比较(孩子比较父亲) struct Compare { bool operator()(const pair<string, int>& p1, const pair<string, int>& p2) { return p1.second < p2.second || (p1.second == p2.second && p1.first > p2.first); } }; string s; getline(cin, s); vector<string> word; string tmp; for (size_t i = 0; i < s.size(); ++i) { if(s[i] != ' ' && s[i] != '.') { tmp += tolower(s[i]); } else { if(tmp.size() >= 1) word.push_back(tmp); tmp.clear(); } } // 创建一个哈希表 unordered_map<string, int> hash; for(auto e : word) { hash[e]++; } // 创建优先级队列 priority_queue<pair<string, int>, vector<pair<string, int>>, Compare> q(hash.begin(), hash.end(), Compare()); for(size_t i = 0; i < hash.size(); ++i) { cout << q.top().first << ":" << q.top().second << endl; q.pop(); } } // 64 位输出请用 printf("%lld")
from collections import Counter word=input().split() # 去句号 if word[-1][len(word[-1])-1] =='.': word[-1]=word[-1][0:len(word[-1])-1] # 转小写 words = [i.lower() for i in word] # 统计词频 dic = Counter(words) dic=sorted(dic.items(),key=lambda x:(x[0],x[1]),reverse=True) #for kv in dic: #print('{}:{}'.format(kv[0],kv[1])) # 反向遍历 for x in dic[::-1]: print("{}:{}".format(x[0],x[1]))
#include <iostream> #include <string> #include <algorithm> #include <map> #include <vector> using namespace std; struct Word{ string str; int cnt; }; bool cmp(Word lhs, Word rhs){ return lhs.cnt>rhs.cnt; } int main(){ string str, temp; map<string, int> mymap; while(str.back()!='.'){ cin >> str; if(str.front()>='A'&& str.front()<='Z'){ string rep; rep.push_back(str.front()-'A'+'a'); str.replace(0,1, rep); } temp = str; if(temp.back()=='.') temp.pop_back(); mymap[temp]++; } vector<Word> res; Word t; for(auto it=mymap.begin(); it!=mymap.end(); it++){ t.str = it->first; t.cnt = it->second; res.push_back(t); } sort(res.begin(), res.end(), cmp); for(int i=0; i<res.size(); ++i){ cout << res[i].str << ":" << res[i].cnt << endl; } }
#include<iostream> #include<map> #include<vector> #include<algorithm> #include<string> using namespace std; //仿函数控制比较 struct comp { bool operator()(const pair<string, int>& kv1, const pair<string, int>& kv2) { return kv1.second > kv2.second || (kv1.second == kv2.second && kv1.first < kv2.first); } }; int main() { //输入字符串 string s; getline(cin, s); //转小写 for (auto& ch : s) { if (ch >= 'A' && ch <= 'Z') ch += 32; } //将单词放进数组 vector<string> v; for (int i = 0; i < s.size(); ++i) { string tmp; while(s[i] != ' ' && s[i] != '.') { tmp += s[i]; ++i; } v.push_back(tmp); } //map统计次数 map<string, int> m; for (auto ch : v) { m[ch]++; } //sort+仿函数排序 vector<pair<string, int>> vv(m.begin(), m.end()); sort(vv.begin(), vv.end(), comp()); //输出 for (int i = 0; i < vv.size(); ++i) { cout << vv[i].first << ":" << vv[i].second << endl; } return 0; }
//用map来排序很容易 #include <iostream> #include <map> using namespace std; int main() { string s1; string s2; map<string,int> m1; getline(cin,s1); for(const auto& e : s1) { if(e == ' '|| e == '.') { m1[s2]++; s2.clear(); continue; } s2.push_back(tolower(e)); } for(const auto& e: m1) { cout<<e.first<<":"<<e.second<<endl; } return 0; }
#include <iostream> #include <string> #include <map> #include <algorithm> #include <vector> using namespace std; struct Compare { bool operator()(const pair<int, string>& l, const pair<int, string>& r) { return l.first > r.first || (l.first == r.first && l.second < r.second); } }; int main() { string input; map<string, int> words; while (cin >> input) { //转小写 transform(input.begin(), input.end(), input.begin(), ::tolower); //去除小写字母以外的输入 string s; for (const auto& e : input) { if (e >= 'a' && e <= 'z') s.push_back(e); } words[s]++; } vector<pair<int, string>> v; for (const auto& e : words) { v.push_back(make_pair(e.second, e.first)); } sort(v.begin(), v.end(), Compare()); for (const auto& e : v) { cout << e.second << ":" << e.first << endl; } }
#include <iostream> #include <cstring> using namespace std; int main() { char ch[1000][100]; int n; //单词个数 for (int i = 0; i < 1000; i++) { cin >> ch[i]; for (int j = 0; j < strlen(ch[i]); j++) { //大写字母转小写 ch[i][j] = tolower(ch[i][j]); } if (ch[i][strlen(ch[i]) - 1] == '.') { //字符串以“.”结尾 ch[i][strlen(ch[i]) - 1] = '\0'; //将末尾的“.”截断 n = i + 1; break; } } //冒泡排序法将字符串排序 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (strcmp(ch[i], ch[j]) < 0) { char temp[100]; strcpy(temp, ch[i]); strcpy(ch[i], ch[j]); strcpy(ch[j], temp); } } } for (int i = 0, count = 1; i < n; i++) { if (!strcmp(ch[i], ch[i + 1])) { count++; } else { cout << ch[i] << ":" << count << endl; count = 1; } } }
#include<iostream> #include<string> #include<map> using namespace std; int main() { string str; map<string, int> mp; while (getline(cin, str)) { int slow = 0; //单词的起始下标 string s; //存放单个单词 for (int i = 0; i < str.size(); i++) { //大写变小写 if ('A' <= str[i] && str[i] <= 'Z') { str[i] += 32; } //取单词放入mp中,插入+计数+自动降序排序 if (str[i] == ' ' || str[i] == '.') { int num = i - slow; //单词大小 s = str.substr(slow, num); //通过单词起始下标和大小取出来 mp[s]++; slow = i + 1; //跳过空格到下一个单词的起始位置 } } //上述已经按字典序排好了---下面将次数大的放在前面 //注意次数出现相等,防止去重,要用multimap //手动更改为降序 multimap<int, string, greater<int>> intsort; for (const auto& e : mp) { //插入+排序 intsort.insert(make_pair(e.second, e.first)); } for (const auto& e : intsort) { cout << e.second << ":" << e.first << endl; } } return 0; }
#include<string> #include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; typedef map<string,int>::iterator mapIterator; struct cmpIterator { bool operator()(const mapIterator& it1, const mapIterator& it2) { return it1->second > it2->second; } }; int main() { string str; getline(cin,str); //应该先把对应大写转成小写; for(int i = 0; i < str.size();i++) { // if(isupper(str[i])){ // tolower(str[i]); // } if (str[i] >= 65 && str[i] <= 90) { str[i] += 32; } } vector<string> vWord; size_t startPos = 0; size_t endPos = 0; while(endPos != str.npos) { endPos = str.find(' ',startPos); vWord.push_back(str.substr(startPos,endPos - startPos)); startPos = endPos + 1; } for(auto& word:vWord) { for(int i = 0; i < word.size();i++) { if(word[i] == '.') { word.erase(word.size() - 1); } } } //处理完毕,下面统计次数 map<string,int> countMap; for(auto word : vWord) { countMap[word]++; } //map<string,int>::iterator it = countMap.begin(); mapIterator it = countMap.begin(); vector<mapIterator> vPtr; while(it != countMap.end()) { vPtr.push_back(it); it++; } stable_sort(vPtr.begin(),vPtr.end(),cmpIterator()); for(auto iterator:vPtr) { cout<<iterator->first<<":"<<iterator->second<<endl; } return 0; }分享一个超越%0用户提交的代码,果然优秀的人走哪都是优秀的<狗头>