zoukankan      html  css  js  c++  java
  • Codeforces Round #479 (Div. 3) B. Two-gram

    原题代码:http://codeforces.com/contest/977/problem/B

    题解:有n个字符组成的字符串,输出出现次数两个字符组合。例如第二组样例ZZ出现了两次。

    方法:比较无脑,本来想用map,发现不会用map排序,字符串最长100,然后我选择了暴力。TAT建立一个结构体,把两个字母前后放到a,b,然后统计这样情况出现的次数,最后排序就好啦~

    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<iostream>
    #include<map>
    #include<vector>
    #include<set>
    #include<queue>
    using namespace std;
    struct sub
    {
        char a;
        char b;
        int count;
        sub()
        {
            count = 0;
        }
    }str[100];
    bool compare(sub x, sub y)
    {
        return x.count > y.count;
    }
    int main()
    {
        int n; 
        char s[200];
        cin >> n;
        cin >> s;
        str[0].a = s[0];
        str[0].b = s[1];
        str[0].count = 1;
        int sum = 1;
        for (int i = 1; i < n - 1; i++)
        {
            int flag = 0;
            for (int j = 0; j < i; j++)
            {
                if (str[j].a == s[i] && str[j].b == s[i + 1])
                {
                    flag = 1;
                    str[j].count++;
                    continue;
                }
            }
            if (flag == 0)
            {
                str[sum].a = s[i];
                str[sum].b = s[i + 1];
                str[sum].count++;
                sum++;
            }
        }
        sort(str, str + n + 1, compare);
        printf("%c%c
    ", str[0].a, str[0].b);
        return 0;
    }
  • 相关阅读:
    字典-字典练习
    元组
    切片
    列表-列表练习
    一个登录小程序
    py定义变量-循环-条件判断
    charles抓包
    接口测试-Http状态码-postman上传文件
    jm解决乱码问题-参数化-数据库操作-文件上传下载
    (二)CRLF注入
  • 原文地址:https://www.cnblogs.com/Tangent-1231/p/9005412.html
Copyright © 2011-2022 走看看