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

这道题让你统计一段文字中,数一下哪个单词最多,这里的单词是字母+数字的组合,所以上面的示例是5个can和1个can1
因为这里不区分大小写,所以我们统一转成小写,用的是下的tolower函数,用来判断数字和字符的函数时isalnum()函数,如果是数字or字符就返回true,也是头文件下的函数。
用map来存每个单词的数量
这道题我们用的方法是用getline读取一整行,如果读到字符,就将其转为小写,并加入temp字符的末尾,直到读到不是数字or字符的东西,如果temp字符不为空,就将map对应的值++,并把temp重置为空字符。

#include<map>
#include<cctype>
#include<iostream>
using namespace std;
int main(){
    string s, temp = "";
    map<string, int> ma;
    getline(cin, s);
    for(int i = 0;i < s.length(); i++){
        if(isalnum(s[i])){
            s[i] = tolower(s[i]);
            temp += s[i];
        }
        if(!isalnum(s[i]) || i == s.length() -1){
            if(temp.length() != 0) ma[temp]++;
            temp = "";
        }
    }
    int maxn = 0;
    for(map<string, int>::iterator it = ma.begin(); it != ma.end(); it++){
        if(it->second > maxn){
            temp = it->first;
            maxn = it->second;
        }
    }
    cout<<temp<<" "<<maxn;
    return 0;
}

每次碰到空格,就会再次输入一个字符串,所以在while(1)里面写即可,读到换行符时退出输入,换行符是在每次输入完字符串后用getchar来读取的。如果当前字符大写,就将其转换为小写,如果是字母or数字就将其加入temp,如果都不是,就将这个单词直接加1,并把temp设为空字符串。
每次输入完后就将temp对应的值+1,防止整一个字符串都没有非法字符,所以要在后面再加一次。
然后遍历map,找出值最多的那个字符串,因为我们弄了很多空字符串进去,所以每次碰到空字符串,我们就直接continue
记录空字符的情况1.最后一个字符是非法字符,所以我们在下面将这个空字符串加1
2.连续两个都是非法字符

#include<iostream>
#include<map>
using namespace std;
int main(){
    string a;
    map<string,int> ma;
    while(1){
        cin>>a;
        string temp = "";
        for(int i = 0; i < a.length(); i++){
            if(a[i] >= 'A' && a[i] <= 'Z') a[i] = a[i]-'A'+'a';
            if((a[i] >='a' && a[i] <='z') || (a[i] >='0' && a[i] <='9')) temp += a[i];
            else{//一整个字符串里不会有空格,这个else是碰到非数字or字符了,所以前面的temp已经完成了
                ma[temp] += 1;
                temp = "";
            }
        }
        //每次循环就相当于输入完一个单词,判断句子是否已经结束,或者这个单词可能没有非法字符,没有被计算上,要在这里给它加1
        //如果上面最后一个字符是非法的,那么temp现在就是空字符,在最后遍历map时碰到空字符记得退出
        ma[temp] +=1;
        char c = getchar();
        if(c == '\n') break;
    }
    int cnt = 0;
    string res;
    for(map<string,int>::iterator it = ma.begin(); it!=ma.end(); it++){
        if(it->first == "") continue;
        if(it -> second > cnt){
            cnt = it->second;
            res = it->first;
        }
    }
    cout<<res<<" "<<cnt;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值