Speech Patterns (25)

版权声明:本文章由BlackCarDriver原创,欢迎评论和转载 https://blog.csdn.net/BlackCarDriver/article/details/89115716

链接:https://www.nowcoder.com/pat/5/problem/4120
Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can help to narrow down a speaker’s identity, which is useful when validating, for example, whether it’s still the same person behind an online avatar.
Now given a paragraph of text sampled from someone’s speech, can you find the person’s most commonly used word?

输入描述:

Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return ‘\n’. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

输出描述:

For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a “word” is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.
Note that words are case insensitive.
输入例子:
Can1: “Can a can can a can? It can!”
输出例子:
can 5


大意描叙:
给出一行带空格和符号的英文语句,你需要从中找出出现频率最多的单词,若有多个单词频率相同则输出字典序最小的一个。
注意事项:单词是又数字或大小写字母组成的长度最小为一的片段,对大小写不敏感

做题回顾
it problem purpose on test the coder ablity of handle a string. When i see it problem frist time, I am worry about misunderstanding of the meaning of the problem and also worry on out of time. I see the input example and thought
that I should pick up substring betweend tow ’ " ’ fristly yet the problem text do not describe about that, it made me wonder
a long time. I know i have to spild the long string into many different word, but I can’t not make up an good way to do that
with c++ std::string. Lastly, I use char array to easliy handle it.

解决思路
这条题目就是考察字符串的处理能力,很重要的还有对英语题目的理解能力。本来想用string做,但是发现想要预处理和
切割字符串不太方便,所以还是用字符数组来接收输入。得到输入后先进行简单预处理,除掉无效字符,将大写变成小写。
然后分割成单独的单词,用map来记录每个单词的出现频率。最后就是排序了,直接排序后第一个即是字典序最小的字符串。

My Code:

#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 1048579;
char input[maxn];

int check(char c){
	if (('0' <= c && c <= '9') || ('a' <= c && c<='z') || ('A' <= c && c <= 'Z')){
		if ((c >= 'A' && c <= 'Z')) return 1;	//需要转成小写
		return 2;
	}
	return -1;//无效字符用空格代替
}

int main(){
	cin.getline(input, maxn);
	//预处理字符串
	for (int i = 0; input[i] != '\0'; i++){
		int res = check(input[i]);
		if (res == -1) input[i] = ' ';
		else if (res == 1) input[i] -= ('A'-'a');
	}
	//逐个取出单词,加入map
	map<string, int>simap;
	string temp;
	int maxnum = 0;
	for (int i = 0; input[i] != '\0'; i++){
		if (input[i] == ' ' && temp != ""){
			simap[temp]++;
			maxnum = max(maxnum, simap[temp]);
			temp = "";
		}
		if(input[i]!=' ')temp.push_back(input[i]);
	}
	//找到字典序最小的那个字符串
	for (auto i : simap){
		if (i.second == maxnum){
			cout << i.first <<" "<< maxnum << endl;
			return 0;
		}
	}
}

回顾备忘录
1.用string 接收整行输入的方法为: getline(cin,s);
2.用迭代器迭代map,string就是按照字典序遍历,得到的第一个string直接作为答案即可,无需排序。

猜你喜欢

转载自blog.csdn.net/BlackCarDriver/article/details/89115716