Timus 1723 Sandro's Book



Description

It's been quite a number of years since Lich Sandro retired. Sometimes in the evenings, when he feels especially lonely, he takes a book that was presented to him by his student magicians on the occasion of his retirement.
This evening the great magician is also reading the book. One of the chapters describes Sandro's famous discovery: he invented a universal spell many years ago. Any substring (a few consecutive symbols of the string) of the universal spell is also a spell, and its power is equal to the number of times this spell is encountered in the universal spell (for example, the string “ue” encounters in the string “queue” twice, and the string “aba” encounters in the string “abababa” three times).
Sandro has a lot of free time now and he wants to find the most powerful spell. Help Sandro do it.

Input

The only input line contains the universal spell invented by Sandro. The spell is a non-empty string consisting of lowercase English letters with length at most 50.

Output

Output any of the most powerful spells, according to Sandro's definition.
sample input   
         tebidohtebidoh (input)  -------->  tebidoh(output)

 题意:给你一段字符串找到里面出现最多的子串。开始看了sample input 的样例,把题意弄错了,其实一个字符就是一个字串啊,并不需要kmp算法,最后只用了个map记录下或者开个arr[26]的数组把a~z字符出现的次数都记录下在比较都是可行的。

题意理解对这题就是一道水题,以此记录在熟悉下map的用法。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
	string a;
	int max=0;
	cin>>a;
	map<char,int>mp;
	for(int i=0;i<a.length();i++)
	{
	    mp[a[i]]++;
	}
	map<char,int>::iterator p1,p2;   //p1的作用就是遍历Map
	
	p2=mp.begin(); //p2的作用就是最后指向出现次数最多的字符,将其输出
	
	//找到出现次数最多的字符,也就是字串
	for(p1=mp.begin();p1!=mp.end();p1++)
	{
		if(p1->second>max)
		{
			max=p1->second;
			p2=p1;
		}
	}
	cout<<p2->first<<endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值