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

思路分析

1、使用哈希表存储字典

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

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

4、对次序排序再输出

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

代码如下:

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

转载于:https://www.cnblogs.com/tallisHe/p/4055580.html

poj 1035 Spell checker(hash)相关推荐

  1. POJ 1035, Spell checker

    brutal force 1. 生成字典, 并按长度排序. 2. 对于给定单词, 取得区间 [单词长度 - 1, 单词长度 + 1], 并匹配. 3. 如果存在完全匹配的单词, 打印输出, 否则把可能 ...

  2. POJ 2341 Spell checker 笔记

    找出句首单词首字母小写和大写字母不是单词的第一个字母的错误.

  3. android studio spellchecker,Exclude files from Android Studio lint spell checker

    问题 The Android Studio lint spell checker flags hex codes that look to it like words in certain files ...

  4. 论文阅读-FASPell: A Fast, Adaptable, Simple, Powerful Chinese Spell Checker Based

    来源:爱奇艺 EMNLP2019 Workshop 论文:https://aclanthology.org/D19-5522.pdf 代码:GitHub - iqiyi/FASPell: 2019-S ...

  5. vscode php代码检测,VSCODE代码拼写检查插件Code Spell Checker

    Code Spell Checker是一款VSCODE代码拼写检查插件,可以为开发者报告一些常见的拼写错误.这个拼写检查程序的目标是帮助捕获常见的拼写错误,同时保持低误报的数量.它很适合驼峰式代码. ...

  6. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

  7. poj 2503(字符串hash)

    解题思路(copy别人的):通过ELFhash函数来做.冲突处理则是用到链表的方法. #include <iostream> #include <fstream> #inclu ...

  8. POJ-1035 Spell checker 暴力

    直接暴力. 代码如下: #include <cstring> #include <cstdio> #include <cstdlib> #include <m ...

  9. html标签属性可以省略,html部分---通用标签与属性;

    body的属性: bgcolor页面背景色:text文字颜色:topmargin上页边距:leftmargin左页边距:rightmargin右页边距:bottomargin下页边距: src是引用过 ...

最新文章

  1. 图片增强来解决图片不足问题
  2. Spring boot自动配置模式
  3. 安卓使用jdbc连接mysql_Android:使用JDBC连接MySql
  4. [NOIP1999] 提高组 洛谷P1014 Cantor表
  5. 判断字符串是否为正整数 浮点小数
  6. oracle实现mysql的if_oracle中decode函数 VS mysql中的if函数和case函数
  7. Prometheus-使用Prometheus监控Kubernetes集群
  8. pandas 画折线图_如何从Pandas数据帧绘制多个折线图
  9. 解读年度数据库PostgreSQL:如何处理并发控制(一)
  10. STM32 IIC详解
  11. 监控、链路追踪、日志的区别
  12. Linux内核:VFIO Mediated Device(vfio-mdev)内核文档(翻译)
  13. Python文件操作小案例:交替合并两个记事本文件
  14. 在使用Git提交代码的时候犯了个低级错误
  15. bcm94352屏蔽第20针脚图_10核20线程的性能怪兽 十代酷睿首发评测
  16. 脚本——LoadRunner文件操作函数详解
  17. 微信支付H5完整版代码
  18. petalinux install
  19. mac苹果电脑如何读取ntfs格式软件?
  20. Low Power概念介绍Level Shifter

热门文章

  1. Sharepoint学习笔记—Delegate Control--在Sharepoint页面添加jQuery
  2. 使用thead,tbody,tfoot来实现表格的分页打印
  3. 【零基础学Java】—笔记本USB接口案例(二十八)
  4. JavaScript学习(四十四)—in关键字的使用和for in循环遍历对象的属性
  5. linux系统电脑的权限设置,Linux下的文件权限设置修改详解linux操作系统 -电脑资料...
  6. if vue 跳出_vue实现弹框遮罩点击其他区域弹框关闭及v-if与v-show的区别介绍
  7. Java String类的相关操作
  8. 商业医疗险住院报销需要什么材料?
  9. 请问孩子厌学的话该怎么办?
  10. 湿气重的人,脸上会有哪些信号?