题目链接http://poj.org/problem?id=1035

思路分析

1、使用哈希表存储字典

2、对待查找的word在字典中查找,查找成功输出查找成功信息

3、若查找不成功,对word增、删、改处理,然后在字典中查询,若查找成功则记录处理后单词在字典中的次序

4、对次序排序再输出

注:对word处理后可能会出现重复,需要进行判断重

代码如下:

#include <iostream>
using namespace std; const int N = ;
const int tSize = ;
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 = ; for ( int i = ; i < len; ++i )
hashVal = ( hashVal * + key[i] - 'a' ) % tSize; while ( stateDic[hashVal] != &&
strcmp( hashDic[hashVal], key ) != )
{
hashVal = ( hashVal + ) % tSize;
} if ( stateDic[hashVal] == )
{
stateDic[hashVal] = ;
strcpy( hashDic[hashVal], key );
rankDic[hashVal] = pos;
return true;
} return false;
} int InsertWords( char *key )
{
int len = strlen( key );
unsigned int hashVal = ; for ( int i = ; i < len; ++i )
hashVal = ( hashVal * + key[i] - 'a' ) % tSize; while ( stateW[hashVal] !=
&& strcmp( words[hashVal], key ) != )
{
hashVal = ( hashVal + ) % tSize;
} if ( stateW[hashVal] == )
{
stateW[hashVal] = ;
strcpy( words[hashVal], key );
return true;
} return false;
} int Find( char *key )
{
int len = strlen( key );
unsigned int hashVal = ; for ( int i = ; i < len; ++i )
hashVal = ( hashVal * + key[i] - 'a' ) % tSize; while ( stateDic[hashVal] != &&
strcmp( hashDic[hashVal], key ) != )
{
hashVal = ( hashVal + ) % tSize;
} if ( stateDic[hashVal] == )
return -;
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 = ;
char word[N]; memset( stateDic, , sizeof( stateDic ) );
while ( scanf( "%s", word ) == )
{
if ( word[] == '#' )
break; InsertDic( word, countDic++ );
} while ( scanf( "%s", word ) == )
{
char copy[N];
int indexWord;
int len = strlen( word ); if ( word[] == '#' )
break; ansLen = ;
memset( stateW, , sizeof( stateW ) );
printf( "%s", word ); indexWord = Find( word );
if ( indexWord > - )
{
printf( " is correct\n" );
continue;
} for ( int i = ; i <= len; ++i )
{
int j; strcpy( copy, word );
for ( j = len; j >= i; --j )
copy[j + ] = copy[j];
for ( char a = 'a'; a <= 'z'; ++a )
{
copy[i] = a; indexWord = Find( copy );
if ( indexWord > - && InsertWords( copy ) )
Ans[ansLen++] = indexWord;
}
} for ( int i = ; i < len; ++i )
{
strcpy( copy, word );
for ( int j = i + ; j <= len; ++j )
copy[j - ] = copy[j]; indexWord = Find( copy );
if ( indexWord > - && InsertWords( copy ) )
Ans[ansLen++] = indexWord;
} for ( int i = ; i < len; ++i )
{
strcpy( copy, word );
for ( char w = 'a'; w <= 'z'; ++w )
{
copy[i] = w; indexWord = Find( copy );
if ( indexWord > - && InsertWords( copy ) )
Ans[ansLen++] = indexWord;
}
} qsort( Ans, ansLen, sizeof( Ans[] ), cmp );
printf( ":" ); for ( int i = ; i < ansLen; ++i )
printf( " %s", hashDic[Ans[i]] );
printf( "\n" );
} return ;
}

poj 1035 Spell checker(hash)的更多相关文章

  1. poj 1035 Spell checker ( 字符串处理 )

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16675   Accepted: 6087 De ...

  2. poj 1035 Spell checker

    Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   J ...

  3. [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 De ...

  4. poj 1035 Spell checker(水题)

    题目:http://poj.org/problem?id=1035 还是暴搜 #include <iostream> #include<cstdio> #include< ...

  5. POJ 1035 Spell checker 字符串 难度:0

    题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...

  6. POJ 1035 Spell checker(串)

    题目网址:http://poj.org/problem?id=1035 思路: 看到题目第一反应是用LCS ——最长公共子序列 来求解.因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O( ...

  7. POJ 1035 Spell checker (模拟)

    题目链接 Description You, as a member of a development team for a new spell checking program, are to wri ...

  8. POJ 1035 Spell checker 简单字符串匹配

    在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...

  9. 【POJ】1035 Spell checker

    字典树. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib ...

随机推荐

  1. linux下emacs安装

    1.下载地址:http://ftp.gnu.org/pub/gnu/emacs/ 下载文件:emacs-24.2.tar.gz 步骤: 一.安装依赖文件:  (先进入root:终端中输入 su -) ...

  2. iOS网络开发-AFNetworking请求asp.net WebService

    看到园子有位朋友需要使用AFN框架请求 WebService,所以就整理了一下,demo下载链接在底部 编写WebService可以看这篇博客 http://www.cnblogs.com/linmi ...

  3. mvc模式jsp+servel+dbutils oracle基本增删改查demo

    mvc模式jsp+servel+dbutils oracle基本增删改查demo 下载地址

  4. ELK(ElasticSearch, Logstash, Log4j)系统日志搭建

    1.elk平台介绍 Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logsta ...

  5. BZOJ 2329: [HNOI2011]括号修复( splay )

    把括号序列后一定是))))((((这种形式的..所以维护一个最大前缀和l, 最大后缀和r就可以了..答案就是(l+1)/2+(r+1)/2...用splay维护,O(NlogN). 其实还是挺好写的, ...

  6. sqlserver 在将 nvarchar 值 'XXX' 转换成数据类型 int 时失败

    最近做oracle和sqlserver数据库兼容,感觉sqlserver真心没oracle好用,存储过程竟然只能返回int类型,疯了 疯了 存储过程的output及return的区别 sql取整 ce ...

  7. win7下文件名不能定义为con(任何文件格式)

    从linux传输压缩包到win7下解压缩,总是提示出错,可是在linux下解压都很正常,于是定位出错的文件,发现是con.c和con.h文件,经排查,原因如下: CON是DOS下的特殊设备名 如下由系 ...

  8. [转]android Handler使用

    转 http://blog.csdn.net/new_abc/article/details/8184634 不过这个我看不懂 不知道为什么i的值可以接着增长... package com.examp ...

  9. Windows10笔记本双显卡导致的启动黑屏解决办法之一

    参考链接:http://www.zhihu.com/question/33662311 大概就是关掉ulps. ulps,显卡的多核心超低功率状态,节能用的,AMD出的双显卡的一种节能方案.不过,与某 ...

  10. VC使用CRT调试功能来检测内存泄漏

    信息来源:csdn     C/C++ 编程语言的最强大功能之一便是其动态分配和释放内存,但是中国有句古话:“最大的长处也可能成为最大的弱点”,那么 C/C++ 应用程序正好印证了这句话.在 C/C+ ...