深信服:输入一个字符串,帮忙统计字符串里面的每个单词出现的次数,以及非法单词的次数。非法单词的定义为:包含数字(0-9)的单词
生活随笔
收集整理的這篇文章主要介紹了
深信服:输入一个字符串,帮忙统计字符串里面的每个单词出现的次数,以及非法单词的次数。非法单词的定义为:包含数字(0-9)的单词
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
深信服2018秋招筆試題:
輸入一個(gè)字符串,幫忙統(tǒng)計(jì)字符串里面的每個(gè)單詞出現(xiàn)的次數(shù),以及非法單詞的次數(shù)。非法單詞的定義為:包含數(shù)字(0-9)的單詞
輸入一個(gè)字符串,長(zhǎng)度小于1000,輸入的字符僅包含(0-9,a-z,A-Z, . , : ! )及空格。
輸出按單詞的字母進(jìn)行排序,每行為?:
單詞??? 單詞出現(xiàn)次數(shù)
最后一行輸出非法單詞個(gè)數(shù)
實(shí)例1:
輸入:
wow! Is wonderful!
輸出:
Is 1
wonderful 1
wow 1
0
實(shí)例2:
輸入:
abc bcd2 fe fe a3xd
輸出:
abc 1
fe 2
2
代碼如下:
#include<iostream> #include<map> #include<string> #include<vector>using namespace std; int main() {string str;map<string, int>temp;getline(cin, str);int plow = 0, phigh = 0;string s = "";while (phigh < str.length()){if (str[phigh] == ' '){phigh++;plow = phigh;temp[s]++;s = "";continue;}else{s = s + str[phigh];if (phigh == str.length() - 1) temp[s]++;phigh++;}}vector<string>res;auto j = temp.begin();while(j != temp.end()){string tmp = j->first;int len = tmp.size();for (int i = 0; i < len; i++){if (!(tmp[i] >= 65 && tmp[i] <= 90 || tmp[i] >= 97 && tmp[i] <= 122)){if (tmp[i] >= 48 && tmp[i] <= 57){res.push_back(tmp);j = temp.erase(j);j--;break;}}else if (tmp[len-1] == 44 || tmp[len-1] == 46 || tmp[len-1] == 58 || tmp[len-1] == 33) {string new_tmp(tmp.begin(), tmp.end() - 1);int b = j->second;temp.insert(make_pair(new_tmp, b));j = temp.erase(j);j--;break;}}j++;}for (auto s = temp.begin(); s != temp.end(); s++){cout << s->first << " " << s->second << endl;}cout << res.size() << endl;system("pause");return 0; }主要是對(duì)問題的仔細(xì)分析!然后就是注意細(xì)節(jié),特別是迭代器的失效問題,位置變換等問題!
總結(jié)
以上是生活随笔為你收集整理的深信服:输入一个字符串,帮忙统计字符串里面的每个单词出现的次数,以及非法单词的次数。非法单词的定义为:包含数字(0-9)的单词的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 11.C++: cin、cin.get(
- 下一篇: map:根据 value 找 key ?