Skip to content

USACO 2016 December Contest Bronze Division - Block Game#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

For each word, find its frequency and for each pair of words, add to the final answer the biggest frequency for each letter there.

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ifstream cin("blocks.in");
    ofstream cout("blocks.out");

    vector<int> total(26);

    int n;
    cin >> n;

    for(; n; --n)
    {
        string a, b;
        cin >> a >> b;
        vector<int> frq(26), frq2(26);
        for(auto x : a)
            frq[x - 'a']++;
        for(auto x : b)
            frq2[x - 'a']++;
        for(int i = 0; i < 26; i++)
            total[i] += max(frq[i], frq2[i]);
    }

    for(int i = 0; i < 26; i++)
        cout << total[i] << '\n';
    return 0;
}
with open("blocks.in", "r") as fin:
    n = int(fin.readline().strip())
    total = [0] * 26
    for _ in range(n):
        a, b = fin.readline().split()
        frq = [0] * 26
        frq2 = [0] * 26
        for ch in a:
            frq[ord(ch) - ord('a')] += 1
        for ch in b:
            frq2[ord(ch) - ord('a')] += 1
        for i in range(26):
            total[i] += max(frq[i], frq2[i])
with open("blocks.out", "w") as fout:
    for count in total:
        fout.write(str(count) + "\n")