HrbustOJ 1109 店长终极推荐

店长终极推荐

Time Limit: 2000 MS Memory Limit: 65536 K

Total Submit: 743(214 users) Total Accepted: 261(195 users) Rating: Special Judge: No

Description

玩腻了两个工具之后,店长决定要好好学习.玩工具伤身体啊!!店长决定教大家学习阅读,店长总能总学习中找到乐趣,无聊的他找了一篇文章,由各种字符组成,他想统计下这篇阅读中,出现最多次数的相邻两个字符组合是什么?

Input

第一行输入一个整数T表示测试数据组数

接下来输入一行,包括一篇文章,文章由最多不超过200个字符(由任意ascii字符组成)

当T=0时结束

Output

统计文章中出现频度最高的两个连续字符,并且输入然后换行.

如果最高的有多组,按照字典顺序排序后输出第一个.

每T组测试数据后,需要输出一个换行

Sample Input

1
%#@!%#
2
This is a test!
Dianzhang JiaoZhu and Jiashou

Sample Output

%#

is
an

Hint

店长自此开始学习复习考研了,一个神秘人物出现了,传说中的叫兽??敬请期待

Author

void

题解

此题可以用map容器来解决,map容器本身会给键排序,所以正好可以用map来简化做题过程。键为存取的两个字符所组成的字符串,值为出现的次数,使用一个变量记录最大值,之后扫描这个最大值并获得所对应的键并输出就能得到结果。代码如下:

#include <iostream>
#include <cstdio>
#include <set>
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>

using namespace std ;

const int MOD = 1e6 ;

int main(){
    int t ;
    while ( cin >> t && t ){
        getchar() ;
        map<string , int> map_ ;
        map<string , int>::iterator it ;
        while ( t -- ){
            map_.clear() ;
            string str ;
            getline(cin , str) ;
            int maxx = -1 ;
            for ( int i = 0 ; i < str.size() - 1 ; i ++ ){
                string index = str.substr(i , 2) ;  //截取str从i开始n个字符的子串
                map_[index] ++ ;
                maxx = max(map_[index] , maxx) ;
            }
            for ( it = map_.begin() ; it != map_.end() ; it ++ ){
                if ( it->second == maxx ){
                    cout << it->first << endl ;
                    break ;
                }
            }
        }
        cout << endl ;
    }
    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/Cantredo/p/9769893.html