PKU 1013

Counterfeit Dollar

12枚のコインそれぞれが軽い・重いと仮定し、矛盾がないか調べる。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    cin >> n;

    string info[] = { "down", "even", "up" };

    for ( int i=0; i<n; i++ )
    {
        string weight[3][3];

        for ( int j=0; j<9; j++ )
            cin >> weight[0][j];

        for ( char c='A'; c<='L'; c++ )
        for ( int w=-1; w<=1; w+=2 )
        {
            bool ok = true;;

            for ( int j=0; j<3; j++ )
            {
                int t = 0;
                if ( weight[j][0].find( c ) != string::npos )
                    t += w;
                if ( weight[j][1].find( c ) != string::npos )
                    t -= w;

                if ( weight[j][2] != info[t+1] )
                    ok = false;
            }

            if ( ok )
                cout << c << " is the counterfeit coin and it is "
                << ( w<0 ? "light" : "heavy" ) << "." << endl;
        }
    }

    return 0;
}