1071 Speech Patterns (25分)

1 题目

1071 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?

Input Specification:
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].

Output Specification:
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.

Sample Input:
Can1: "Can a can can a can?  It can!"

      
    
Sample Output:
can 5

2 解析

2.1 题意

输入一行语句,判断出现最多的单词(单词和大小写字母的混合体)以及它的出现次数

2.2 思路

对输入的字符判断是否在单词字符(‘a~z’,‘A~Z’,‘1~9’),如果是则拼接单词,如果不是跳过非单词字符。

用map<\string,int>映射单词和出现次数

3 参考代码

#include <iostream>
#include <map>
#include <string>

using std::cin; using std::cout;
using std::endl; using  std::string;
using std::map;

map<string, int> count;

bool Check(char c){//检测是否在单词要求范围内
    if(c >= '0' && c <= '9') return true;
    else if(c >= 'a' && c <= 'z') return true;
    else if(c >= 'A' && c <= 'Z') return true;
    else return false;
}

int main(int argc, char const *argv[])
{
    string str, words;
    getline(cin, str);

     int i = 0;
     while(i < str.length())
     {
         words.clear();//清空上一个存留的单词

         while(i < str.length() && Check(str[i]) == true){//如果是单词
             if(str[i] >= 'A' && str[i] <= 'Z'){//大写转换为小写
                 str[i] = str[i] - 'A' + 'a';//str[i] += 32;
             }
             words += str[i];
             i++;
         }

         if(words != ""){//单词非空,令次数加1
             if(count.find(words) == count.end()){
                 count[words] = 1;
             }else{
                 count[words]++;
             }
         }

         while(i < str.length() && Check(str[i]) == false){//跳过非单词字符
             i++;
         }

     }

//    for(int i = 0; i < str.length(); i++){
//        if(Check(str[i]) == false){//非单词字符
//            if(words == ""){
//                continue;
//            }else{
//                if(count.find(words) == count.end()){//该单词没有出现过
//                    count[words] = 1;
//                }else{//单词出现过
//                    count[words]++;
//                }
//                words.clear();//清空该一个单词,为下一次单词输入做准备
//            }
//        }else{//单词字符
//            if(str[i] >= 'A' && str[i] <= 'Z'){
//                str[i] += 32;
//            }
//            words += str[i];
//        }
//
//        if(i == str.length() - 1){//特判,当末尾没有非单词的字符,而字符串自后一个是单词的一部分
//            if(words != ""){
//                if(count.find(words) == count.end()){
//                    count[words] = 1;
//                }else{
//                    count[words]++;
//                }
//            }
//        }
//    }

    string ans;
    int times = 0;
    for (map<string, int>::iterator it = count.begin(); it != count.end() ; ++it)
    {
        if(it->second > times){
            times = it->second;
            ans = it->first;
        }
    }

    cout << ans << " " << times << endl;

    return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页