PAT 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

题目大意:给出一个字符串,找出其中出现最多次数的word,这个word是由字母数字组成的。并且使大小写不敏感的。

下面代码在牛客网上AC,但是PAT上最后一个测试点没通过,答案错误。

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
string lowCase(string s){
    for(int i=0;i<s.size();i++){
        if(s[i]>=‘A‘&&s[i]<=‘Z‘){
            s[i]=s[i]-‘A‘+‘a‘;//如何将大写转换为小写呢?
        }
    }
    return s;
}
int main() {
    string s="";
    map<string,int> mp;
    char ch=getchar();
    while(ch!=‘\n‘){
        if(isdigit(ch)||isalpha(ch)){
            s+=ch;//加到字符串后。
        }else {
            if(s!=""){
                mp[lowCase(s)]++;//放入map
               // cout<<s<<" ";
                s="";//被赋值为空。
            }
        }
        ch=getchar();
    }
    int mx=0;
    for(auto it=mp.begin();it!=mp.end();it++){
        if(it->second>mx){
            mx=it->second;
            s=it->first;
        }
    }
    cout<<s<<" "<<mx;
    return 0;
}

果然发现了问题:

运行结果应该是3,而不是2。

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
string lowCase(string s){
    for(int i=0;i<s.size();i++){
        if(s[i]>=‘A‘&&s[i]<=‘Z‘){
            s[i]=s[i]-‘A‘+‘a‘;//如何将大写转换为小写呢?
        }
    }
    return s;
}
int main() {
    string s="";
    map<string,int> mp;
    char ch=getchar();
    while(ch!=‘\n‘){
        if(isdigit(ch)||isalpha(ch)){
            s+=ch;//加到字符串后。
            //因为map不进去。
        }else {
            if(s!=""){
                mp[lowCase(s)]++;//放入map
               // cout<<s<<" "<<mp[s]<<‘\n‘;
                s="";//被赋值为空。
            }
        }
        //cout<<ch;
        ch=getchar();
        if(ch==‘\n‘){//加上这个判断就AC了~~~
            if(s!="")mp[lowCase(s)]++;//因为如果最后一个算不上。
        }
    }
    int mx=0;
    //cout<<‘\n‘;
    for(auto it=mp.begin();it!=mp.end();it++){
        if(it->second>mx){
            mx=it->second;
            s=it->first;
        }
    }
    cout<<s<<" "<<mx;
    return 0;
}
// can can can

//学习了,这个字符串处理~~~

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9785103.html

时间: 2024-11-10 17:08:19

PAT 1071 Speech Patterns[一般]的相关文章

PAT 1071. Speech Patterns (25)

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 ident

PAT 1071. Speech Patterns

又是考输入输出 #include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; char buf[1048577]; bool is_alphanumerical(char &ch) { if (ch >=

PAT:1071. Speech Patterns (25) AC

#include<ctype.h> #include<iostream> #include<map> #include<string> using namespace std; bool check(char a) //判断是否为有效字符 { if(isdigit(a) || isalpha(a)) return true; return false; } int main() { map<string,int> count; //单词与次数的映

PAT (Advanced Level) 1071. Speech Patterns (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

1071. Speech Patterns (25)

map的使用 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming 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

1071 Speech Patterns

题意:给出一个字符串,找出词频最高的单词和相应的词频.(这个就是我之前做的一个项目的一个函数啊,哈哈哈) 思路:利用map定义字典map<string,int> dict.主要是字符串的截取,这里用上了几个比较方便的函数,总结如下: 几个有助于节省编码时间的字符串处理函数(在头文件<ctype.h>或<cctype>下) isalnum 判断字符是否为字母(含大小写)或数字 isalpha 判断字符是否为字母(含大小写) islower 判断字符是否为小写字母 isup

PATA 1071 Speech Patterns.

#include <bits/stdc++.h> using namespace std; bool check(char c)//检查是否为字母或数字 { if(c>='A'&&c<='Z'||c>='a'&&c<='z'||c>='0'&&c<='9') return true; else return false; } int main() { map<string,int> count; s

pat1071. Speech Patterns (25)

1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops&qu

PAT 1071. 小赌怡情

PAT 1071. 小赌怡情 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注t个筹码后,计算机给出第二个数.若玩家猜对了,则系统奖励玩家t个筹码:否则扣除玩家t个筹码. 注意:玩家下注的筹码数不能超过自己帐户上拥有的筹码数.当玩家输光了全部筹码后,游戏就结束. 输入格式: 输入在第一行给出2个正整数T和K(<=100),分别是系统在初始状态下赠送给玩家的筹码数.以及需要处理的游戏次数.随后K行,每行