Bitmask 연산자를 이용해서 풀었고..
아샬님이 16-bit 시스템인 경우를 지적해주셔서 mask table을 unsigned char 배열로 정의했습니다.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
void setMask( char c, char * pTbl )
{
c = toupper( c ) - 'A';
pTbl[ c / 8 ] |= ( 0x1 << ( c % 8 ) );
}
void setLetterTable( const char * s, char * pTbl )
{
while ( *s ) setMask( *s++, pTbl );
}
int isLetterBank( const char * bank, const char * str )
{
unsigned char tbl[2][4] = { { 0 } };
setLetterTable( bank, tbl[0] );
setLetterTable( str, tbl[1] );
return ( strcmp( tbl[0], tbl[1] ) == 0 );
}
void test()
{
assert( isLetterBank( "IMPS", "MISSISSIPPI" ) );
assert( isLetterBank( "BLUE", "BLUE" ) );
assert( ! isLetterBank( "CNUT", "COCONUT" ) );
assert( ! isLetterBank( "IPC", "PC" ) );
}
int main( int argc, char * argv[] )
{
#ifdef __TEST__
test();
#endif
if ( argc == 3 )
printf( "%s\n", ( isLetterBank( argv[1], argv[2] )? "YES" : "NO" ) );
return 0;
}
다음글 지뢰찾기 2 (J) | 김창준