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 >= ‘0‘ && ch <= ‘9‘) return true;
    if (ch >= ‘a‘ && ch <= ‘z‘) return true;
    if (ch >= ‘A‘ && ch <= ‘Z‘) {
        ch += ‘a‘ - ‘A‘;
        return true;
    }
    return false;
}

int main() {

    char ch;

    bool inword = false;
    int wpos = 0;
    int max_count = -1;

    unordered_map<string, int> count;
    vector<string> maxs;

    while(ch = getchar()) {
        bool isan = is_alphanumerical(ch);
        if (isan) {
            if (!inword) {
                // new word begin
                wpos     = 0;
                inword     = true;
            }
            // append character
            buf[wpos++] = ch;
        } else {
            if (inword) {
                // current word end
                buf[wpos] = ‘\0‘;
                string word(buf);
                auto iter = count.find(word);
                if (iter == count.end()) {
                    iter = count.insert(make_pair(word, 0)).first;
                }
                if (++(iter->second) > max_count) {
                    max_count = iter->second;
                    maxs.clear();
                    maxs.push_back(word);
                } else if (iter->second == max_count) {
                    maxs.push_back(word);
                }
                inword = false;
            }
        }
        if (ch == ‘\n‘) {
            break;
        }
    }

    sort(maxs.begin(), maxs.end());
    int mcount = count.find(maxs[0])->second;
    printf("%s %d", maxs[0].c_str(), mcount);
    return 0;
}
时间: 2024-08-14 17:01:12

PAT 1071. Speech Patterns的相关文章

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

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 (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行,每行