1071. Speech Patterns

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1071


/*
 * map:自动创建机制。
 * transform:将某操作应用于指定范围的每个元素,类似于foreach。
 * 定义在头文件algorithm中。
 * ::tolower:将一个字符大写变小写。
 */ 

#pragma warning(disable: 4786)
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#define SIZE 1048576+10

using namespace std;

char buf[SIZE];

bool is_alpha(char ch)
{
    if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
        return true;
    else
        return false;
}

int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("E:\\in.txt", "r", stdin);
    #endif

    gets(buf);
    string str = buf;
    transform(str.begin(), str.end(), str.begin(), ::tolower);

    map<string, int> m;
    int s, e, i;
    i = 0;
    int max=-1;
    string max_str = "";
    while ( i < str.size())
    {
        while(!is_alpha(str[i]) && i < str.size())
            i++;
        s = i;
        //
        while(is_alpha(str[i]) && i < str.size())
            i++;
        e = i;
        string word = str.substr(s, e-s);
        m[word]++;//word,若未出现,会自动创建
        if(m[word] > max || (m[word] == max && max_str > word))
        {
            max = m[word];
            max_str = word;
        }
    }
    cout << max_str << " " << max << endl;

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值