121

코딩 도장

일주일에 하나씩 가벼운 마음으로 코드를 작성합니다. 누가 떠밀지 않아도 스스로 자라길 원하는 사람들...
클럽장: 아샬 | http://club.filltong.net/codingdojo
최근 방문자들   모든회원보기
풀이 풀이
Letter Bank (C)  

2008. 06. 24 23:13   |   조회수:97
CoMKiD CoMKiD
http://club.filltong.net/codingdojo/4336  복사

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;
}

댓글 1

  • hash,set 이 기본으로 제공되지 않을 때 간단한 hash 대용으로서 bitmask 를 쓸 수 있겠군요. 2008. 06. 25 10:49
등록
목록 맨뒤로 글쓰기
다음글다음글   지뢰찾기 2 (J)   |   
이전글이전글   n차원 지뢰찾기 입니다.(common lisp)   |