Question Link: Http://poj.org/problem? Id = 1035
Ideas:
1. Use a hash table to store dictionaries
2. Search the word in the dictionary. if the search is successful, the search result is displayed.
3. If the search fails, add, delete, and modify the word, and query it in the dictionary. if the search is successful, record the order of the word in the dictionary after processing.
4. sort and output the order
Note: Word processing may repeat and requires heavy judgment.
Code:
#include <iostream>using namespace std;const int N = 50;const int tSize = 12007;char hashDic[tSize][N];int stateDic[tSize];int rankDic[tSize];char words[tSize][N];int stateW[tSize];int Ans[tSize];int ansLen;int InsertDic( char *key, int pos ){ int len = strlen( key ); unsigned int hashVal = 0; for ( int i = 0; i < len; ++i ) hashVal = ( hashVal * 26 + key[i] - ‘a‘ ) % tSize; while ( stateDic[hashVal] != 0 && strcmp( hashDic[hashVal], key ) != 0 ) { hashVal = ( hashVal + 1 ) % tSize; } if ( stateDic[hashVal] == 0 ) { stateDic[hashVal] = 1; strcpy( hashDic[hashVal], key ); rankDic[hashVal] = pos; return true; } return false;}int InsertWords( char *key ){ int len = strlen( key ); unsigned int hashVal = 0; for ( int i = 0; i < len; ++i ) hashVal = ( hashVal * 26 + key[i] - ‘a‘ ) % tSize; while ( stateW[hashVal] != 0 && strcmp( words[hashVal], key ) != 0 ) { hashVal = ( hashVal + 1 ) % tSize; } if ( stateW[hashVal] == 0 ) { stateW[hashVal] = 1; strcpy( words[hashVal], key ); return true; } return false;}int Find( char *key ){ int len = strlen( key ); unsigned int hashVal = 0; for ( int i = 0; i < len; ++i ) hashVal = ( hashVal * 26 + key[i] - ‘a‘ ) % tSize; while ( stateDic[hashVal] != 0 && strcmp( hashDic[hashVal], key ) != 0 ) { hashVal = ( hashVal + 1 ) % tSize; } if ( stateDic[hashVal] == 0 ) return -1; else return hashVal;}int cmp( const void *a, const void *b ){ int *pA = (int *)a; int *pB = (int *)b; return rankDic[*pA] - rankDic[*pB];}int main( ){ int countDic = 0; char word[N]; memset( stateDic, 0, sizeof( stateDic ) ); while ( scanf( "%s", word ) == 1 ) { if ( word[0] == ‘#‘ ) break; InsertDic( word, countDic++ ); } while ( scanf( "%s", word ) == 1 ) { char copy[N]; int indexWord; int len = strlen( word ); if ( word[0] == ‘#‘ ) break; ansLen = 0; memset( stateW, 0, sizeof( stateW ) ); printf( "%s", word ); indexWord = Find( word ); if ( indexWord > -1 ) { printf( " is correct\n" ); continue; } for ( int i = 0; i <= len; ++i ) { int j; strcpy( copy, word ); for ( j = len; j >= i; --j ) copy[j + 1] = copy[j]; for ( char a = ‘a‘; a <= ‘z‘; ++a ) { copy[i] = a; indexWord = Find( copy ); if ( indexWord > -1 && InsertWords( copy ) ) Ans[ansLen++] = indexWord; } } for ( int i = 0; i < len; ++i ) { strcpy( copy, word ); for ( int j = i + 1; j <= len; ++j ) copy[j - 1] = copy[j]; indexWord = Find( copy ); if ( indexWord > -1 && InsertWords( copy ) ) Ans[ansLen++] = indexWord; } for ( int i = 0; i < len; ++i ) { strcpy( copy, word ); for ( char w = ‘a‘; w <= ‘z‘; ++w ) { copy[i] = w; indexWord = Find( copy ); if ( indexWord > -1 && InsertWords( copy ) ) Ans[ansLen++] = indexWord; } } qsort( Ans, ansLen, sizeof( Ans[0] ), cmp ); printf( ":" ); for ( int i = 0; i < ansLen; ++i ) printf( " %s", hashDic[Ans[i]] ); printf( "\n" ); } return 0;}
Poj 1035 spell checker