poj 1035 Spell checker(hash)
题目链接: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)的更多相关文章
- poj 1035 Spell checker ( 字符串处理 )
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16675 Accepted: 6087 De ...
- poj 1035 Spell checker
Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u J ...
- [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18693 Accepted: 6844 De ...
- poj 1035 Spell checker(水题)
题目:http://poj.org/problem?id=1035 还是暴搜 #include <iostream> #include<cstdio> #include< ...
- POJ 1035 Spell checker 字符串 难度:0
题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...
- POJ 1035 Spell checker(串)
题目网址:http://poj.org/problem?id=1035 思路: 看到题目第一反应是用LCS ——最长公共子序列 来求解.因为给的字典比较多,最多有1w个,而LCS的算法时间复杂度是O( ...
- POJ 1035 Spell checker (模拟)
题目链接 Description You, as a member of a development team for a new spell checking program, are to wri ...
- POJ 1035 Spell checker 简单字符串匹配
在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...
- 【POJ】1035 Spell checker
字典树. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib ...
随机推荐
- PendingIntent详解
Intent是一个意图,一个描述了想要启动一个Activity.Broadcast或是Service的意图.它主要持有的信息是它想要启动的组件(Activity.Broadcast或是Service) ...
- c# 遍历文件夹及其所有文件
利用VS创建一个winform应用程序,遍历指定文件夹(photos)内的所有文件夹及其文件.具体程序如下: namespace 遍历文件夹及其所有文件 { public partial class ...
- 五毛的cocos2d-x学习笔记06-处理用户交互
前几篇感觉自己在写教育文章,╮(╯▽╰)╭.今天换成开发者的口吻,毕竟我也是在边学边写博客. 处理用户交互包括:单点触摸.多点触摸.事件传递.传感器.物理按键等部分. 单点触摸: 触摸事件传递顺序 o ...
- Java虚拟机体系结构
转自:http://www.cnblogs.com/java-my-life/archive/2012/08/01/2615221.html JAVA虚拟机的生命周期 一个运行时的Java虚拟机实例的 ...
- JS学习之prototype属性
javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用.以A.prototype = new B();为例, 理 ...
- QT实现,通过URL下载文件的接口实现
今天来把坑填上. 具体就是提供一个URL,并通过这个URL下载文件. MyDownloader.h: #ifndef MYDOWNLOADER_H #define MYDOWNLOADER_H cla ...
- C/C++中的内存管理
一.内存的分配方式 1. 程序代码区 2. 静态数据区 3. 动态数据区 二.动态内存 1. 在栈上创建的内存 2. 从堆上分配的内存 3. 小结 三.指针与内存 1. 操作内存 2. 指针与数组 3 ...
- Windows Azure 网站上的 WordPress 3.8
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 和 Windows Azure 网站开发人员体验合作伙伴共同撰写. WordPr ...
- Linux 下的多线程编程
随着你对编程的深入,多线程是一个免不了的话题,在这里就对多线程做一个比较详细的总结. 首先摆在我们面前的就是什么是线程,以及为么会有这个东西.记得之前学习的时候自己会画一张很大的图,在图中可以详细的写 ...
- JAVA GUI学习 - 总结
一:项目 二:重要组件补充 三:组件高级操作