zoukankan      html  css  js  c++  java
  • 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  . 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!='
    '){
            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!='
    '){
            if(isdigit(ch)||isalpha(ch)){
                s+=ch;//加到字符串后。
                //因为map不进去。
            }else {
                if(s!=""){
                    mp[lowCase(s)]++;//放入map
                   // cout<<s<<" "<<mp[s]<<'
    ';
                    s="";//被赋值为空。
                }
            }
            //cout<<ch;
            ch=getchar();
            if(ch=='
    '){//加上这个判断就AC了~~~
                if(s!="")mp[lowCase(s)]++;//因为如果最后一个算不上。
            }
        }
        int mx=0;
        //cout<<'
    ';
        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

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

      

  • 相关阅读:
    [Windows] 输入字符间距变宽
    [Android] 安卓手机不用root删除自带app
    [Linux] 内核通知链 notifier
    [RK3399] ES8316+NS4150 播放视频只有背景音,播放歌曲有的有声音,有的无声音
    [Linux] RTC 读写指令及测试程序
    [Linux] scp指令用法
    [Ubuntu] sudo apt-get update指令执行失败
    [RK3399] 修改移动网络默认为4G
    [RK3288] 外接USB设备出现丢数
    laravel 模拟数据批量添加
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9785103.html
Copyright © 2011-2022 走看看