题解 | #单词识别#

单词识别

https://www.nowcoder.com/practice/16f59b169d904f8898d70d81d4a140a0

#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

int main(){
	set<string> st;
	map<string,int> mp;
	string tmp;
	vector<string> v;
	bool flag = true;
	
	while(flag && cin>>tmp){
		if(tmp[tmp.size()-1] == '.'){
			tmp = tmp.substr(0,tmp.size()-1);
//			cout<<tmp<<"------------"<<endl;
			flag = false;
		}
		if(tmp[0] >= 'A' && tmp[0] <= 'Z')
			tmp[0] = tmp[0] + 32;
//		cout<<tmp;
		v.push_back(tmp);
		st.insert(tmp);
	}
	for(set<string>::iterator it = st.begin(); it != st.end(); it++){
		for(int i = 0; i < v.size(); i++){
			if(*it == v[i])
				mp[*it]++;
		}
	}
	
	for(map<string,int>::iterator it = mp.begin(); it != mp.end(); it++){
		cout<<it->first<<":"<<it->second<<endl;
	}
	
}

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务