이번 주는 불타오르는군요!!
원래 C로 짜도 되지만.. 코드 읽기 좋으라고 C++로 짰어요.
bitmask를 이용하니 별다른 문자열 처리 없이도 간단하게 되네요. :)
(뭐 다른 언어를 사용하신 분들에 비하면 여전히 긴..)
입력에 대해서는 대소문자는 구분하지 않았구요.
대문자든 소문자든 첫번째 입력이 두번째 입력에 모두 포함되면 YES 처리 했어요.
이번엔 assert를 이용해서 test case도 넣었습니다~
- #include <iostream>
#include <cassert>
using namespace std;
#define MASK( c ) ( 0x1 << (int) ( toupper( (c) ) - 'A' ))
class Bank
{
public :
Bank( const char * str ) : table( 0 )
{
while ( *str )
table |= MASK( *str++ );
}
bool includes( const char * sub ) const
{
while( *sub && ! ( table & MASK( *sub++ ) ) )
return false;
return true;
}
private :
int table;
friend void testBank();
};
void testBank()
{
Bank b( "abc" );
assert( b.table == ( 0x1 | 0x2 | 0x4 ) );
assert( b.includes( "bc" ) == true );
assert( b.includes( "d" ) == false );
Bank s1( "MISSISSIPPI" );
assert( s1.includes( "IMPS" ) == true );
Bank s2( "Blue" );
assert( s2.includes( "BLUE" ) == true );
Bank s3( "COCONUT" );
assert( s3.includes( "CunT" ) == true );
Bank s4( "PC" );
assert( s4.includes( "ipc" ) == false );
}
int main( int argc, char * argv[] )
{
#ifdef __TEST__
testBank();
#else
if ( argc == 3 )
{
Bank b( argv[2] );
cout << ( b.includes( argv[1]) ? "YES" : "NO" ) << endl;
}
#endif
return 0;
}