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;
}
原文地址:https://www.cnblogs.com/Tangent-1231/p/9005412.html