Codeforces 977B Two-gram(stl之string掉进坑)
Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams.
You are given a string ss consisting of nn capital Latin letters. Your task is to find any two-gram contained in the given string as a substring(i.e. two consecutive characters of the string) maximal number of times. For example, for string ss = "BBAABBBA" the answer is two-gram "BB", which contained in ss three times. In other words, find any most frequent two-gram.
Note that occurrences of the two-gram can overlap with each other.
The first line of the input contains integer number nn (2≤n≤1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.
Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string ss as a substring (i.e. two consecutive characters of the string) maximal number of times.
7
ABACABA
AB
5
ZZZAA
ZZ
In the first example "BA" is also valid answer.
In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.
思路:用map<string, int>维护一个最大值输出即可
坑点:string的加法为string = string + char* , 不是string = char* + char* 或者char + char!
反正第一个加号前面必须得是string!
然后就是学一下map的一些操作了,注意map迭代器表示key和value的方式
代码:
#include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
typedef long long ll;
using namespace std;
const int maxn = 100000 + 100;
int main(){
map<string, int> m;
int n;
scanf("%d", &n);
getchar();
string s;
cin >> s ;
//int len = s.length();
string ans;
int maxx = 0;
for(int i = 0; i < n-1; i++){
string ss;
ss =ss+ s[i] + s[i+1];
//cout << ss<<endl;
if(!m.count(ss))m[ss] = 0;
m[ss]++;
//if(m[ss] > )
}
map<string, int>::iterator it;
map<string, int>::iterator itt;
for( it = m.begin(); it != m.end(); it++){
if(it->second > maxx){
itt = it;
maxx = it->second;
//cout << it->second << endl;
}
}
cout <<itt->first;
return 0;
}
Codeforces 977B Two-gram(stl之string掉进坑)的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- C++ string中的几个小陷阱,你掉进过吗?
C++开发的项目难免会用到STL的string,使用管理都比char数组(指针)方便的多,但在得心应手的使用过程中也要警惕几个小陷阱,避免我们项目出bug却迟迟找不到原因. 1. 结构体中的stri ...
- [Codeforces 1246B] Power Products (STL+分解质因数)
[Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...
- 用Unity代码通过Xml配置生成GameObject之——前两天掉的坑
1. Resources.Load(path),path不是绝对路径,而是相对"Resources/"的相对路径!如: 要想Instantiate则代码应该如下: string m ...
- luogu P4688 [Ynoi2016]掉进兔子洞 bitset 莫队
题目链接 luogu P4688 [Ynoi2016]掉进兔子洞 题解 莫队维护bitset区间交个数 代码 // luogu-judger-enable-o2 #include<cmath&g ...
- loj #6201. 「YNOI2016」掉进兔子洞
#6201. 「YNOI2016」掉进兔子洞 您正在打galgame,然后突然发现您今天太颓了,于是想写个数据结构题练练手: 给出一个长为 nnn 的序列 aaa. 有 mmm 个询问,每次询问三个区 ...
- 【手记】小心在where中使用NEWID()的大坑 【手记】解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题 【C#】组件分享:FormDragger窗体拖拽器 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
[手记]小心在where中使用NEWID()的大坑 这个表达式: ABS(CHECKSUM(NEWID())) % 3 --把GUID弄成正整数,然后取模 是随机返回0.1.2这三个数,不可能返回其它 ...
- YNOI2016:掉进兔子洞 (莫队+bitset)
YNOI2016:掉进兔子洞 题意简述: 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于这 ...
- [Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset)
[Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset) 题面 一个长为 n 的序列 a.有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间 ...
随机推荐
- ACID特性及幻读的理解
事务是关系型数据库的重要特性.它是一个包含了一条或多条SQL语句的逻辑原子单元.一个事务包含的SQL要么全部提交,要么全部回滚. Oracle 官方文档中对事务的描述如下: A transaction ...
- redis 为什么是单线程,为什么速度快。
redis 5中存储方式 String.List.Set.Hash.ZSet这5种 数据库的工作模式按存储方式可分为: 硬盘数据库和内存数据库.Redis 将数据储存在内存里面,读写数据的时候都不会受 ...
- Mybatis-plus 实体类继承关系 插入默认值
在实际开发中,会定义一些公共字段,而这些公共字段,一般都是在进行操作的时候由程序自动将默认值插入.而公共的字段一般会被封装到一个基础的实体类中,同时实体类中会实现相应的getter setter 方法 ...
- 初始 Kafka Consumer 消费者
温馨提示:整个 Kafka 专栏基于 kafka-2.2.1 版本. 1.KafkaConsumer 概述 根据 KafkaConsumer 类上的注释上来看 KafkaConsumer 具有如下特征 ...
- esri mdb 数据库导入 到postgreSQL
需求: 项目升级,需要将esri的个人数据库(mdb格式)导入到开源数据库postgreSQL中. 思路: 使用fwtools工具导出到数据库中. 环境: windows+fwtools+postgr ...
- 【转】在Ubuntu下建立Eclipse的Android开发环境
本文将介绍如何建立Ubuntu下基于Eclipse的Android开发环境的方法. 大部分的Android开发者都是使用Eclipse来开发Android,本文将向各位介绍一下建立Ubuntu下基于E ...
- 枚举 + exgcd
题意:已知xi=(a*xi-1+b) mod 10001,且告诉你x1,x3.........x2*t-1,让你求出其偶数列 思路分析 : 题目所要求的的是对 10001 取余,由模运算的性质可知,a ...
- python-review01
# 1.使用while循环输出 1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 if count == 7: continue ...
- Docker windows nano server容器中安装ssh实现远程登录管理
[问题] 使用ServiceMonitor.exe作为前台进程运行起来的容器无法attach. 无法远程连接到运行中的容器中进行管理. [解决方法] 在container中新建管理员用户,通过SSH实 ...
- linux DHCP 服务器
配置 1:/etc/dhcp.conf 配置文件 2:dhcp.leases 启动 dhcp 服务器 linux dhcp客户端 windows dhcp 客户端