1071. Speech Patterns (25)解题报告

来源:互联网 发布:java实现自定义表单 编辑:程序博客网 时间:2024/06/06 12:50
#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstdlib>#include <map>#include <cctype>#include <string>#include <iostream>using namespace std;const int N = 1048576;int main(void) {    map<string, int> mp;    map<string, int>::iterator it;    char *word = new char[N], tmp;    int i = 0, cnt = -1;    string str, maxstr;    do {        scanf("%c", &tmp);        if (isalpha(tmp) || isdigit(tmp)) {            word[i] = tolower(tmp);            i++;        }        else if(i) {            word[i] = '\0';            i = 0;            str = string(word);            it = mp.find(str);            if (it == mp.end()) {                mp[str] = 1;                if (cnt < 1) {                    cnt = 1;                    maxstr = str;                }                else if(cnt == 1) {                    if (str < maxstr) {                        maxstr = str;                    }                }            }            else {                mp[str]++;                if (mp[str] > cnt) {                    cnt = mp[str];                    maxstr = str;                }                else if (mp[str] == cnt) {                    if (str < maxstr) {                        maxstr = str;                    }                }            }        }    } while (tmp != '\n');    cout << maxstr << ' ' << cnt << endl;    delete[] word;    return 0;}
0 0
原创粉丝点击