题目链接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. FusionCharts 学习总结

    FusionCharts和Charts一样都是对数据进行统计并绘制成图标的控件,但FusionCharts带有跨浏览器的flash图表组件解决方案,功能更为强大.. 在这里我将采用Js来加载Fusio ...

  2. [Jobdu] 题目1361:翻转单词顺序

    题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“stu ...

  3. JavaSE_ API常用对象 总目录(11~14)

    JavaSE学习总结第11天_开发工具 & API常用对象111.01 常见开发工具介绍11.02 Eclipse和MyEclipse的概述11.03 Eclipse的下载安装及卸载11.04 ...

  4. chrome extension overview

    目录 什么是扩展............................................................................................ ...

  5. Matplotlib中文乱码

    想要分析一批数据,画出图形会比较直观.所以就搜索了一下各种软件,最终选择使用python的matplotlib.原因也是因为python使用起来比较方便,虽然R才是分析数据的首选,不过,没有R的基础, ...

  6. MSSQL 如何删除字段的所有约束和索引

    原文MSSQL 如何删除字段的所有约束和索引 代码如下: ---------------------------------------------------------- --  mp_DropC ...

  7. Nginx工作原理和优化、漏洞(转)

    查看安装了哪些模块命令: [root@RG-PowerCache-X xcache]# nginx/sbin/nginx -Vnginx version: nginx/1.2.3built by gc ...

  8. BZOJ 1023

    program bzoj1023; uses math; ; maxn=; maxm=; type edge=record togo,next:longint; end; var n,m,cnt,in ...

  9. swift3.0 hello swift(1)

    一直对swift感兴趣,在前段时间的新闻中,大多是swift3.0发布和xcode8.0的改进,因为改动比较大,以前使用swift2.x做项目的人,都在担心其项目从2.x迁移到3.0+的问题.以前简单 ...

  10. The document "ViewController.xib" could not be opened. Could not read archive.

    The document "ViewController.xib" could not be opened. Could not read archive. Please use ...