1071. Speech Patterns 解析

getline把整行读入,大写变小写,然后去掉标点(标点变空格)

然后分割单词,用set和map建立映射,统计数目。

注意:单个单词的时候,需要单独判定。我是再转换的时候标记又没有空格,没有空格就是单个单词,然后单独输出“该单词 1”。

…………………………更新线………………………………

注意最后没有符号的结尾单词的处理。例如can can can如果依靠非字符判断会少判定一个,单独拿出来就好了。



#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cstring>

#define MAX 1000010

using namespace std;

string s;
string words[MAX];
map <string, int> str2int;
map <int, string> int2str;
set <string> word;
int Count[MAX];


bool convert(string & s) {
	bool tag = true;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] >= 'A' && s[i] <= 'Z') {
			s[i] = 'a' + s[i] - 'A';
		}
		else if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9')) {
		}
		else {
			s[i] = ' ';
			tag = false;
		}
	}
	return tag;
}

int main() {

	memset(Count, 0, sizeof(Count));

	string s;
	getline(cin, s, '\n');

	bool tag = convert(s);



	int pos = 0;
	if (!tag) {
		for (int i = 0; i < s.size(); i++) {
			bool tag = false;
			if (pos > MAX)
				while (1);
			while (s[i] != ' ') {
				words[pos].push_back(s[i++]);
				tag = true;
			}
			if (tag)
				word.insert(words[pos++]);
		}
	}
	else{
		cout << s << " 1" << endl;
		return 0;
	}

	

	int no = 0;
	set <string>::iterator it;
	for (it = word.begin(); it != word.end(); it++) {
		str2int[*it] = no;
		int2str[no++] = *it;
//		cout << *it << endl;
	}

	int max = -1;
	int maxno = 0;
	for (int i = 0; i < pos; i++) {
		int tno = str2int[words[i]];
		Count[tno]++;
		if (max < Count[tno]) {
			max = Count[tno];
			maxno = tno;
		}
	}

	
	cout << int2str[maxno] << " " << Count[maxno] << endl;

	return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值