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 ...
随机推荐
- ubuntu 14.04设备flash媒体播放器
今天是2014年4一个月19日本,就在两天前公布 ubuntu 14.04版本号, 以今天的优势是星期六,西安小雨,所以折腾linux. 我是个有点linux基础的小白.说是有些基础是由于以前在上大学 ...
- Spring 构造注入 传參数
1. 提供相应的构造方法 //构造器注入 public class Bean6 { private String name; private Integer age; // 服务于构造器 ...
- Uploadif稍做扩展使用
文章出自Uploadify扩展配置使用http://www.wuyinweb.com/doc/52/57.aspx 在做项目中涉及多文件上传,经过筛选,选择了Uploaidify,但还涉及一个问题,就 ...
- [01] Preparation - Sitecore Installment
Sitecore CMS 是一套内容管理系统商业软件,其底层平台依托于微软.net技术.由于最近的一个项目采用了这个平台,所以有机会接触到了这个产品. 虽然接触该产品已有一段时间,但总感觉对这个产品缺 ...
- 一个页面从输入URL到页面加载显示完成的详细过程
下面以访问baidu页面来做一个过程分析 输入 URL:http://www.baidu.com DNS 域名解析 计算机无法识别域名,计算机与计算机之间要想进行通信,必须通过ip地址用来定位该计算机 ...
- The Painter's Partition Problem Part II
(http://leetcode.com/2011/04/the-painters-partition-problem-part-ii.html) This is Part II of the art ...
- 【Cocos2D-x 3.5实战】坦克大战(2)游戏开始界面
关于游戏的素材都是在网上到处搜集到的,然后自己再用二流的ps技术修修改改的,所以有可能混在一起有点不搭调(没有办法啊,没有美工Orz.. 项目已经建立好了,然后我们需要把我们下载的素材放到Resour ...
- BZOJ 1296: [SCOI2009]粉刷匠( dp )
dp[ i ][ j ] = max( dp[ i - 1 ][ k ] + w[ i ][ j - k ] ) ( 0 <= k <= j ) 表示前 i 行用了 j 次粉刷的机会能正 ...
- php开发中的一些常用统计的日期
<?php echo '<br>今天:<br>'; echo date("Y-m-d",strtotime("now")), &q ...
- IE 弹出框处理经验
//各屏幕弹出窗样式 // 1366*768var style_1366x768 = "dialogWidth:950px;dialogHeight:650px;help:no;center ...