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 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间 ...
随机推荐
- virtualenv虚拟环境使用及介绍
一.为什么使用virtualenv虚拟环境 每个虚拟环境下的依赖相互独立,不同的项目可以单独使用一套python环境,减少各依赖包的影响 更容易部署 容器化 二.virtualenv创建虚拟环境 三. ...
- 你不得不了解Helm 3中的5个关键新特性
Helm是Kubernetes的一个软件包管理器.两个月前,它发布了第三个主要版本,Helm 3.在这一新版本中,有许多重大变化.本文将介绍我认为最关键的5个方面. 1. 移除了Tiller Helm ...
- C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)
本文介绍通过调用Spire.Cloud.Word.SDK提供的ConvertApi接口将Word转换为PDF.XPS.Epub.RTF以及将Docx转为Doc格式等.调用接口方法及步骤参考以下步骤: ...
- CS0656 缺少编译器要求的成员“Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create”
问题出现原因:在net core使用动态类型dynamic,在编译的时候提示错误信息如上. 解决方案: 1.不用dynamic类型 2.在使用的地方添加一个dll,Microsoft.CSharp,或 ...
- 村庄之间建立邮局 - 区间 dp
There is a straight highway with villages alongside the highway. The highway is represented as an in ...
- 使用RobotFramework的JavaRemoteLibrary
终于被迫使用了Java的远程接口库(为了同时使用Java和python的用例库,且为了在pybot下跑速度能快一些),路途比实际想的要坎坷,记录下来. 远程库的原理在前边一篇文章中记录过: http: ...
- 玩转Django2.0---Django笔记建站基础六(模型与数据库)
第六章 模型与数据库 Django对各种数据库提供了很好的支持,包括:PostgreSQL.MySQL.SQLite和Oracle,而且为这些数据库提供了统一的调用API,这些API统称为ORM框架. ...
- linux DHCP 服务器
配置 1:/etc/dhcp.conf 配置文件 2:dhcp.leases 启动 dhcp 服务器 linux dhcp客户端 windows dhcp 客户端
- Quantitative Trading with R(一):两个简单的策略
下面是两个使用R中的Quantstrat包进行策略构建的例子,都是对600550.ss.600192.ss.600152.ss.600644.ss.600885.ss.600151.ss六只股票进行投 ...
- 宝塔面板管理阿里云服务器FTP不能用
# 宝塔面板管理阿里云,ftp不能用 解决方法 搜ftp点击设置 然后Ctrl+F搜索ForcePassiveIP 注意2在默认情况下是带#号的,去掉#号,后面的ip地址是阿里云的公网ip 重启,再次 ...