unordered_map<string,int>word_count;//空的 stringword; while(cin>>word) { ++word_count[word]; } for(constauto&w:word_count) { cout<<w.first<<" occurs "<<w.second<< ((w.second>1)?" times":" time")<<endl; } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 无序容器...
去除前导零,如果 ptr2 - ptr1>1&&word[ptr1] == '0'将 ptr1前移一位.将区间 [ptr1, ptr2)对应的字符串插入到哈希集合中,最终字符串中不同整数的数目等于哈希集合的元素数目。 */ int numDifferentIntegers(string word) { unordered_set<string> digitnum; int len=word.size(); int ptr1=0,ptr2...
#include <bits/stdc++.h> using namespace std; int main() { string word; set<string> s; while(cin >> word){ for(int i=0;i<word.size();i++) word[i] = tolower(word[i]); regex R("[a-z]{1,}"); smatch Mat; if(regex_search(word, Mat, R)) s.insert(Mat.str()); }...
classSolution{public:vector<string>uncommonFromSentences(string s1, string s2){//加个空格,把两句话合二为一string s=s1+' '+s2;//按空格拆分句子中的单词放到vector里面vector<string> v; string word;for(autoe:s) {if(e!=' ') { word+=e; }else{ v.push_back(word); word.clear(); } ...
Python: Find the longest word in a string I'm preparing for an exam but I'm having difficulties with one past-paper question. Given a string containing a sentence, I want to find the longest word in that sentence and return that word and its ... ...
Python: Find the longest word in a string I'm preparing for an exam but I'm having difficulties with one past-paper question. Given a string containing a sentence, I want to find the longest word in that sentence and return that word and its ... ...
void word_count_pro(std::unordered_map<std::string, int>& m){ std::string word; while (std::cin >> word){ for (auto& ch : word) ch = tolower(ch); word.erase(std::remove_if(word.begin(),word.end(),ispunct),word.end()); ...
map<string,size_t> word_count;//空map,关键字是string,值是size_t类型。 set<string> exclude = {"the","but"};//用set保存想忽略的单词。 string word; while(cin >> word) { //find返回一个迭代器,如果关键字在set中,迭代器指向该关键字,否则返回尾迭代器。
2. map、set系列容器和unordered_map、unordered_set系列容器的区别 首先我们来简单说一下前面学的不带unordered的几个容器和这篇文章学习的unordered系列的容器有什么区别。 首先,它们的底层结构是不一样的: 我们前面学习的那一系列关联式容器——set/multiset 和 map/multimap它们的底层结构是红黑树,而我们这篇文章...
而这道题麻烦的是他给我们的是两个字符串,所以我们要统计单词次数的话可以先按空格把单词分割出来,放到一个vector里面,这样比较好统计。 AC代码 classSolution{public:vector<string>uncommonFromSentences(string s1,string s2){//加个空格,把两句话合二为一...