LoginSignup
0
0

More than 5 years have passed since last update.

第16回オフラインリアルタイムどう書く C++で

Posted at

第16回オフラインリアルタイムどう書くの問題をC++で解いてみた。
@mtsmfm くんに触発されて。約9ヶ月のブランクをへての復帰戦は玉砕でした。
寝る前に30分ほど考えてもまったくアイディアが出てこない。
結局今日の午前ぐらいの時間で解き方を思いつき、40分ぐらいで実装。
ベタです。テーブルをどうにかしたい。二重ループもどうにかしたい。

org16boseg.cpp
#include <string>
#include <algorithm>
#include <sstream>
#include <cstdlib>

static const int table[][6] =
{
    {0,0,0,0,0,0}, {1,0,0,0,0,0}, {1,0,0,0,0,0}, {0,1,0,0,0,0},
    {1,0,0,0,0,0}, {2,0,0,0,0,0}, {0,1,0,0,0,0}, {0,0,1,0,0,0},
    {1,0,0,0,0,0}, {2,0,0,0,0,0}, {2,0,0,0,0,0}, {1,1,0,0,0,0},
    {0,1,0,0,0,0}, {1,1,0,0,0,0}, {0,0,1,0,0,0}, {0,0,0,1,0,0},

    {1,0,0,0,0,0}, {2,0,0,0,0,0}, {2,0,0,0,0,0}, {1,1,0,0,0,0},
    {2,0,0,0,0,0}, {3,0,0,0,0,0}, {1,1,0,0,0,0}, {1,0,1,0,0,0},
    {0,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {0,2,0,0,0,0},
    {0,0,1,0,0,0}, {1,0,1,0,0,0}, {0,0,0,1,0,0}, {0,0,0,0,1,0},

    {1,0,0,0,0,0}, {2,0,0,0,0,0}, {2,0,0,0,0,0}, {1,1,0,0,0,0},
    {2,0,0,0,0,0}, {3,0,0,0,0,0}, {1,1,0,0,0,0}, {1,0,1,0,0,0},
    {2,0,0,0,0,0}, {3,0,0,0,0,0}, {3,0,0,0,0,0}, {2,1,0,0,0,0},
    {1,1,0,0,0,0}, {2,1,0,0,0,0}, {1,0,1,0,0,0}, {1,0,0,1,0,0},

    {0,1,0,0,0,0}, {1,1,0,0,0,0}, {1,1,0,0,0,0}, {0,2,0,0,0,0},
    {1,1,0,0,0,0}, {2,1,0,0,0,0}, {0,2,0,0,0,0}, {0,1,1,0,0,0},
    {0,0,1,0,0,0}, {1,0,1,0,0,0}, {1,0,1,0,0,0}, {0,1,1,0,0,0},
    {0,0,0,1,0,0}, {1,0,0,1,0,0}, {0,0,0,0,1,0}, {0,0,0,0,0,1}
};

void sumAll(const int* input, int* output)
{
    for(int i = 0; i < 5; ++i)
    {
        const int n = input[i] ^ input[i + 1];
        for(int j = 0; j < 6; ++j)
        {
            output[j] += table[n][j];
        }
    }
}

void transpose(int n[6])
{
    int m[6];
    for(int row = 0; row < 6; ++row)
    {
        m[row] = 0;
        for(int col = 0; col < 6; ++col)
        {
            m[row] |= ((n[col] >> row) & 1) << col;
        }
    }

    std::copy(m, m + 6, n);
}

std::string solve(const std::string& input)
{
    int n[6];
    for(int i = 0; i < 6; ++i)
    {
        n[i] = std::strtol(input.substr(i * 2, 2).c_str(), 0, 8);
    }

    int sum[6] = {};
    sumAll(n, sum);
    transpose(n);
    sumAll(n, sum);

    std::ostringstream oss;
    std::copy(sum, sum + 6, std::ostream_iterator<int>(oss, ","));
    std::string result = oss.str();
    return std::string(result.begin(), result.end() - 1);
}
org16bosegTest.cpp
#include <iostream>
#include <string>

std::string solve(const std::string& s);

void test(const std::string& input, const std::string& expected)
{
    std::string output = solve(input);
    std::cout
        << "input:" << input
        << " / output:" << output
        << " / expected:" << expected
        << " / " << (output == expected ? "o" : "x") << std::endl;
}

int main(int, char* [])
{
    /*0*/ test( "060276724276", "6,2,1,1,0,1" );
    /*1*/ test( "770175454177", "2,3,0,3,1,0" );
    /*2*/ test( "743733377170", "9,3,1,0,0,0" );
    /*3*/ test( "724212121273", "5,2,1,1,1,1" );
    /*4*/ test( "100000000000", "3,0,0,0,0,0" );
    /*5*/ test( "000002000000", "4,0,0,0,0,0" );
    /*6*/ test( "003622223600", "0,4,0,4,0,0" );
    /*7*/ test( "520073737070", "8,3,1,1,0,0" );
    /*8*/ test( "770077007700", "0,0,0,0,0,5" );
    /*9*/ test( "555555555514", "2,0,0,0,2,2" );
    /*10*/ test( "764252427600", "4,0,4,0,2,0" );
    /*11*/ test( "774555554177", "3,3,1,3,0,0" );
    /*12*/ test( "674574754557", "11,5,0,1,0,0" );
    /*13*/ test( "000000000000", "0,0,0,0,0,0" );
    /*14*/ test( "777777777777", "0,0,0,0,0,0" );
    /*15*/ test( "774377777577", "6,0,2,0,0,0" );
    /*16*/ test( "070777777777", "0,1,1,0,0,0" );
    /*17*/ test( "373737373737", "0,0,0,0,0,1" );
    /*18*/ test( "603260327725", "30,0,0,0,0,0" );
    /*19*/ test( "466331144663", "30,0,0,0,0,0" );
    /*20*/ test( "000000000242", "3,2,0,0,0,0" );
    /*21*/ test( "567656043772", "18,2,1,0,0,0" );
    /*22*/ test( "200763012420", "15,4,1,0,0,0" );
    /*23*/ test( "400101140052", "14,3,0,0,0,0" );
    /*24*/ test( "764767476476", "13,2,0,1,0,0" );
    /*25*/ test( "001110140110", "12,2,1,0,0,0" );
    /*26*/ test( "765405076527", "16,3,0,1,0,0" );
    /*27*/ test( "377323370373", "8,4,2,0,0,0" );
    /*28*/ test( "250541131216", "11,5,2,0,0,0" );
    /*29*/ test( "744165741476", "12,3,2,0,0,0" );
    /*30*/ test( "042101000300", "10,3,0,0,0,0" );
    /*31*/ test( "002004554101", "11,3,1,0,0,0" );
    /*32*/ test( "371707762706", "15,1,1,0,0,0" );
    /*33*/ test( "130371310175", "7,3,1,2,0,0" );
    /*34*/ test( "212537003613", "13,2,1,1,1,0" );
    /*35*/ test( "157700063411", "15,3,0,0,0,1" );
    /*36*/ test( "011500036007", "6,7,1,0,0,0" );
    /*37*/ test( "743113313517", "17,2,1,0,0,0" );
    /*38*/ test( "174105270405", "13,3,1,1,0,0" );
    /*39*/ test( "427272200311", "13,3,2,0,0,0" );
    /*40*/ test( "725370332237", "12,5,1,1,0,0" );
    /*41*/ test( "005640420046", "12,1,3,0,0,0" );
    /*42*/ test( "700350001101", "14,3,1,0,0,0" );
    /*43*/ test( "577627744076", "16,1,1,1,0,0" );
    /*44*/ test( "620332232007", "10,4,2,1,0,0" );
    /*45*/ test( "260406401000", "15,1,1,0,0,0" );
    /*46*/ test( "737272723276", "5,0,0,0,3,0" );
    /*47*/ test( "000400040444", "7,0,2,0,0,0" );
    /*48*/ test( "370222002177", "13,2,2,0,0,0" );
    /*49*/ test( "372236024656", "9,3,2,0,1,0" );
    /*50*/ test( "276131137003", "11,6,2,0,0,0" );
    /*51*/ test( "742134007240", "13,4,2,0,0,0" );
    /*52*/ test( "777721775571", "13,1,2,0,0,0" );
    /*53*/ test( "700301232233", "11,2,3,0,0,0" );

    return 0;
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0