C++ STL map A1071 Speech Patterns(25) (注意如何从字符串里 分割出单词,注意读取整行带空格的string 需要使用 getlint(cin,str) 函数)

#include <bits/stdc++.h>
#include<math.h>
#include <string>
using namespace std;
const int maxn = 40010;//最大学生人数
bool check(char c){
    if(c >= ‘0‘ && c<= ‘9‘) return true;
    if(c >= ‘A‘ && c<= ‘Z‘) return true;
    if(c >= ‘a‘ && c<= ‘z‘) return true;
    return false;
}
int main(){
    map<string,int> count;//count计数字符串出现的次数
    string str;
    getline(cin,str);
    int i = 0;
    while(i < str.length()){
        string word;
        while(i < str.length() && check(str[i]) == true){//如果是单词的字符
            if(str[i] >= ‘A‘ && str[i] <= ‘Z‘){
                str[i] += 32;//将大写字母转化为小写字母
            }
            word += str[i];
            i++;
        }
        if(word != ""){
            if(count.find(word) == count.end()){
                count[word]  = 1;
            }else{
                count[word]++;
            }
        }
        while(i < str.length() && check(str[i])==false){
            i++;
        }
    }
    string ans;//存放出现次数最多的单词
    int MAX = 0;
    for(map<string,int>::iterator it = count.begin();it != count.end();++it){
        if(it->second > MAX){
            MAX = it->second;
            ans = it->first;
        }
    }
    cout<<ans<<" "<<MAX<<endl;
    system("pause");
    return 0;
} 

原文地址:https://www.cnblogs.com/JasonPeng1/p/12203521.html

时间: 2024-11-05 17:26:16

C++ STL map A1071 Speech Patterns(25) (注意如何从字符串里 分割出单词,注意读取整行带空格的string 需要使用 getlint(cin,str) 函数)的相关文章

C++ STL map B1044/A1100.火星数字(读取带空格的string : 使用getline(cin,str)函数)

用了打表的技巧 #include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int maxn = 40010;//最大学生人数 //[0,12]的火星文 string unitDigit[13] = {"tret","jan","feb","mar","apr"

A1071. 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 v

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

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. 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; //单词与次数的映

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

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

HDU 1004 Let the Balloon Rise【STL&lt;map&gt;】

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123800    Accepted Submission(s): 48826 Problem Description Contest time again! How excited it is to see balloons floating ar