[演習]アルゴリズムのメモはAを発行します。音声パターン(25)

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

//检查单个字符——英文或者数字 
bool check(char ch)
{
	if (ch>='a'&&ch<='z') return true;
	if (ch>='A'&&ch<='Z') return true;
	if (ch>='0'&&ch<='9') return true;
	return false;//合法字符 
}

int main()
{
	map<string,int>mp;
	string s,ans;
	string word;
	getline(cin,s);//输入一串所要求的字符 
	
	for(int i=0; i<s.length(); i++)
	{
		//统计单词 
		if(check(s[i]) == true){
			if(s[i]>='A' && s[i]<='Z')
				s[i] +=32; //转为小写字母
			word += s[i] ;//word字符串加上这个单词 
		}
		
		//非法字符 或者 字符串结束 
		if(check(s[i]) ==false || i==s.length()-1  )
		{
			if(word.length() != 0 )
				mp[word]++; //这个字符串++
			word.clear();//清空 计算下一个字符 
		}
	}
	
	int max=-1; 
	//开始遍历map
	for(map<string,int>::iterator it = mp.begin(); it!=mp.end(); it++) 
		//找出最大的 
		if(it->second>max){
			max = it->second;
			ans = it->first;
		}
		
	cout<<ans<<' '<<max<<endl;
	
}

 

公開された63元の記事 ウォン称賛13 ビュー40000 +

おすすめ

転載: blog.csdn.net/changreal/article/details/88383731
おすすめ