PAT甲级1071 Speech Patterns

本文详细分析了PAT甲级编程题目中的一道问题,强调了审题和理解用例的重要性。作者通过自身的错误经验指出,题目描述往往包含关键信息,如单词统计的定义和特殊情况处理。此外,还分享了在处理输入输出时需要注意的细节,如考虑单词边界、特殊字符和结尾情况。提醒读者,PAT甲级的题目用例可能不具代表性,必须仔细阅读题目要求。
摘要由CSDN通过智能技术生成

1071 Speech Patterns

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
结尾无空行

不知道是不是我的理解有问题,PAT甲级的题目很多时候告诉你的用例非常少,而且没有代表性,根据他给的用例来推断潜在条件完全是错误的,坑

本题注意点:
1、统计单词需要包括发话人:一开始自作聪明,认为统计的只是说出来的话,不包括所谓发话人的名字,结果只过了一个用例,推断“For each test case, print in one line the most commonly occurring word in the input text,”指的是一整句话,出题人根本不是正常人思维

2、单词的划分并不是仅以空格分隔的,还有可能是各种奇怪的符号:推断“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.”,正常人的思维应该是单词都是以空格分隔的,所以并没有太重视这句话,而且给的用例也是空格分隔,没有奇怪字符,所以一开始的我用while(cin >> c)来跳过对空格符的判断,结果有1,2,4用例过不了,正确的应该是用getline输入后逐个判断

3、当单词为空时不要记录在map上

4、输入的末尾可能是字母数字,而不是其他字符:这个是真服了,你以为输入是固定的样式是一个句子形式有双引号的,如果是这样的话你就过不了第三个样例,题目里说单词可能是以结尾分隔,所以得另外判断当句子结尾的情况即使不是奇怪符号也要记录之前的s

教训:PAT甲级的题目一定要审题,用例真没题目重要!!!

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <cctype>
using namespace std;

int main(){
    string s;
    getline(cin,s);
    string temp;
    map<string,int> hash;
    for(int i = 0;i < s.size();++i){
        if(isalnum(s[i])){
            s[i] = tolower(s[i]);
            temp += s[i];
        }
        if(!isalnum(s[i])|| i == s.size() - 1){
            if(!temp.empty()){
                hash[temp]++;
            }
            temp = "";
        }
    }
    
    string res;
    int cnt = -1;
    for(auto j = hash.begin();j != hash.end();++j){
        if(j->second > cnt){
            cnt = j->second;
            res = j->first;
        }
    }
    cout << res << ' ' << cnt;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值