输入为一行,由若干个单词和句号组成
输出格式参见样例。
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 <sstream> using namespace std; #include<map> string upToLow(string &str){//大写变小写 string res=""; for(char c : str){ if (c >= 'A' && c <= 'Z') { c += 32; } res+=c; } return res; } int main() { map<string,int>m; string str; getline(cin,str); str = upToLow(str); stringstream ss(str);//得到小写字符串的输入流 string word; while(ss>>word){ if(word[word.size()-1]=='.'){//去掉句号 word = word.substr(0,word.size()-1); } m[word]+=1; } for(auto a:m){ cout<<a.first<<":"<<a.second<<endl; } }
有人能告知哪里出错了吗?
#include <iostream> #include<string> #include<map> #include<sstream> using namespace std; int main() { string a; map<string,int>mp; getline(cin,a); while(a.find('.')!=-1) a.erase(a.find('.')); istringstream s(a); while(s>>a){ if(a[0]<'a') a[0]=char(a[0]+32); mp[a]++; } for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){ cout<<it->first<<":"<<it->second<<endl; } return 0; }
#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; }