1071 Speech Patterns

题意:给出一个字符串,找出词频最高的单词和相应的词频。(这个就是我之前做的一个项目的一个函数啊,哈哈哈)

思路:利用map定义字典map<string,int> dict。主要是字符串的截取,这里用上了几个比较方便的函数,总结如下:

几个有助于节省编码时间的字符串处理函数(在头文件<ctype.h>或<cctype>下)

  • isalnum 判断字符是否为字母(含大小写)或数字
  • isalpha 判断字符是否为字母(含大小写)
  • islower 判断字符是否为小写字母
  • isupper 判断字符是否为大写字母
  • isdigit 判断字符是否为数字
  • tolower 把大写字母转换成小写字母
  • toupper 把小写字母转换成大写字母

代码:

#include <iostream>
#include <cctype>//isalnum(),tolower()
#include <string>
#include <map>
#include <fstream>
using namespace std;
int main()
{
    //ifstream cin("pat.txt");
    string str;
    getline(cin,str);
    int len=str.size();
    for(int i=0;i<len;i++)
        str[i]=tolower(str[i]);
    map<string,int> dict;//定义字典
    int low=0;
    while(low<len){
        int i=low;
        while(i<len && isalnum(str[i])) i++;
        if(i>low){
            string word=str.substr(low,i-low);
//            if(dict.find(word)==dict.end())//这几句写不写都没关系
//                dict.insert(make_pair(word,1));
//            else
            dict[word]++;
        }
        low=i+1;
    }
    string ans;
    int maxFreq=0;
    for(auto it:dict){
        if(it.second>maxFreq){
            ans=it.first;
            maxFreq=it.second;
        }
    }
    cout<<ans<<‘ ‘<<maxFreq;
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9562714.html

时间: 2024-08-25 03:08:23

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[一般]

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 iden

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

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 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 (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;

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

Speech Patterns (string)

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 v