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

     

  • 相关阅读:
    你知道这高效的12个Java精品库嘛?
    一篇文章带你吃透,Java界最神秘技术ClassLoader
    带你认真了解一下Java分布式系统的基本特性
    因为选定的用户拥有对象,所以无法除去该用户
    mysql自定义函数与过程中写法的注意事项
    使用nssm在windows服务器上部署nodejs
    shopnc 手机网站配置
    关于navicat远程连接mysql问题
    KindEditor 4.1.2版本,在上传图片的时候 设置为绝对路径
    微信分享图标制作
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9552001.html
Copyright © 2011-2022 走看看