yipo
7/17/2017 - 12:59 PM

What's Cryptanalysis?

What's Cryptanalysis?

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class text_analysis
{
public:
    text_analysis() : m_count(128, 0)
    {
    }

    void analyze(const string &str)
    {
        for (auto ch : str)
        {
            if (isalpha(ch)) m_count[toupper(ch)]++;
        }
    }

    vector<pair<char, size_t>> report() const
    {
        vector<pair<char, size_t>> result;

        for (size_t ch = 'A'; ch <= 'Z'; ch++)
        {
            if (m_count[ch] > 0)
            {
                result.push_back(make_pair(ch, m_count[ch]));
            }
        }

        sort(result.begin(), result.end(), [](const pair<char, size_t> &a, const pair<char, size_t> &b)
        {
            return (a.second != b.second) ? a.second > b.second : a.first < b.first;
        });

        return result;
    }

private:
    vector<size_t> m_count;
};

void test_case()
{
    size_t n;
    cin >> n;

    text_analysis p10008;

    cin.ignore(1, '\n');
    while (n--)
    {
        string line;
        getline(cin, line);
        p10008.analyze(line);
    }

    for (auto row : p10008.report())
    {
        cout << row.first << " " << row.second << endl;
    }
}

int main()
{
    test_case();
    return 0;
}