zoukankan      html  css  js  c++  java
  • 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.

    Input

    The first line of the input contains integer number nn (2n1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.

    Output

    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.

    Examples
    input
    Copy
    7
    ABACABA
    
    output
    Copy
    AB
    
    input
    Copy
    5
    ZZZAA
    
    output
    Copy
    ZZ
    
    Note

    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;
    } 

     

  • 相关阅读:
    vue自定义指令clickoutside使用以及扩展用法
    抓包工具Charles的使用教程
    vue之element-ui文件上传
    Vue省市区三级联选择器V-Distpicker的使用
    Vue如何封装多个全局过滤器到一个文件
    vue工程中使用iconfont在线CDN不生效的问题
    es6 Null 传导运算符
    js中布尔值为false的六种情况
    vue-vli3创建的项目配置热更新
    js思维导图
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9552001.html
Copyright © 2011-2022 走看看