LoginSignup
0
0

More than 5 years have passed since last update.

第五回オフラインリアルタイムどう書くの解答(C++)

Last updated at Posted at 2012-11-16

#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>

namespace
{
typedef std::map<std::string, std::string> m_t;
typedef m_t::iterator it_t;
const std::string Jo = "o";

std::string get_hand(const std::string& s, const bool hand)
{
    std::string::const_iterator it = std::find(s.begin(), s.end(), ',');
    return hand ? std::string(++it, s.end()) : std::string(s.begin(), it);
}

bool stronger(const std::string& h, const std::string& f)
{
    const std::string ranks = "3456789TJQKA2o";
    return ranks.find(f) < ranks.find(h);
}

std::string get_field_rank(const std::string& s)
{
    std::string field_rank = s.substr(1,1);
    if(field_rank == Jo)
    {
        if(s.length() == 2)
        {
            return Jo;
        }
        field_rank = s.substr(3,1);
    }
    return field_rank;
}

m_t get_usable_hand(const std::string& h, const std::string& f)
{
    m_t m;
    std::string field_rank = get_field_rank(f);
    if(field_rank == Jo)
    {
        return m;
    }
    for(size_t i=1; i<h.length(); i+=2)
    {
        std::string hand_rank = h.substr(i,1);
        if(stronger(hand_rank, field_rank))
        {
            m[hand_rank] += h.substr(i-1,2);
        }
    }
    const size_t num_of_field_cards =
            m[Jo].empty() ? f.length()/2 : f.length()/2-1;
    for(m_t::const_iterator it = m.begin(); it != m.end(); ++it)
    {
        if(it->second.length()/2 < num_of_field_cards && it->first != Jo)
        {
            m.erase(it->first);
        }
    }
    return m;
}

void depth_first_search(
        int d, int i, const std::string& h, const size_t n,
        const std::string& str, std::set<std::string>& s)
{
    if(d == i)
    {
        if(str.length() == n)
        {
            s.insert(str);
        }
        return;
    }
    depth_first_search(d, i+2, h, n, str, s);
    depth_first_search(d, i+2, h, n, str + h.substr(i, 2), s);
}

std::string solve_impl(const std::string& h, const std::string& f)
{
    std::set<std::string> s;
    depth_first_search(h.length(), 0, h, f.length(), "", s);
    std::string ret = "";
    for(std::set<std::string>::iterator it = s.begin(); it != s.end(); ++it)
    {
        ret += *it + ",";
    }
    return ret;
}

std::string solve(const std::string& s)
{
    std::string hand = get_hand(s,true);
    if(hand.empty())
    {
        return "-";
    }
    std::string field = get_hand(s,false);
    m_t m = get_usable_hand(hand,field);
    std::string ret = "";
    for(it_t it = m.begin(); it != m.end(); ++it)
    {
        if(!m[Jo].empty() && 4 <= field.length() && it->first != Jo)
        {
            it->second += m[Jo];
        }
        ret += solve_impl(it->second, field);
    }
    return ret.empty() ? "-" : ret.substr(0,ret.length()-1);
}
}

BOOST_AUTO_TEST_CASE( solve_test1 )
{
    BOOST_CHECK_EQUAL(solve("DJ,"), "-");
}

0
0
0

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