牛客练习赛13 A 幸运数字Ⅰ 【暴力】

题目链接

https://www.nowcoder.com/acm/contest/70/A

思路
暴力每一个子串 用 MAP 标记一下 然后 最后 遍历一遍 MAP 找出 出现次数最多 并且 字典序最小的那个

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>

using namespace std;
typedef long long LL;

const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;

const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;

int main()
{
    string s, temp;
    cin >> s;
    map <string, int> m;
    m.clear();
    int len = s.size();
    for (int i = 0; i < len; i++)
    {
        if (s[i] == '4' || s[i] == '7')
        {
            temp.clear();
            for (int j = i; j < len; j++)
            {
                temp += s[j];
                m[temp]++;
            }
        }
    }
    map <string, int>::iterator it;
    int Max = -1;
    for (it = m.begin(); it != m.end(); it++)
    {
        if (it -> second > Max)
        {
            temp = it -> first;
            Max  = it -> second;
        }
    }
    if (Max != -1)
        cout << temp << endl;
    else
        cout << -1 << endl;
}
原文地址:https://www.cnblogs.com/Dup4/p/9433260.html