【POJ】1035 Spell checker
字典树。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std; typedef struct Trie {
int in;
Trie *next[];
} Trie; Trie root;
char map[][];
int nums[], nn; void create(char str[], int in) {
int i = , j, id;
Trie *p = &root, *q; while (str[i]) {
id = str[i] - 'a';
++i;
if (p->next[id] == NULL) {
q = (Trie *)malloc(sizeof(Trie));
q->in = -;
for (j=; j<; ++j)
q->next[j] = NULL;
p->next[id] = q;
}
p = p->next[id];
}
p->in = in;
} int find(char str[], int x) {
int i = , id;
Trie *p = &root; while (str[i]) {
if (i == x) {
++i;
continue;
}
id = str[i] - 'a';
++i;
if (p->next[id] == NULL)
return -;
p = p->next[id];
} return p->in;
} void ffind(char str[]) {
int len = strlen(str), i, j, k;
char ch, bk, bf[];
nn = ; for (i=; i<=len; ++i)
bf[i] = str[i];
for (i=; i<len; ++i) {
bk = bf[i];
for (ch='a'; ch<='z'; ++ch) {
if (ch == bk)
continue;
bf[i] = ch;
j = find(bf, -);
if (j != -)
nums[nn++] = j;
}
bf[i] = bk;
} for (i=; i<len; ++i) {
j = find(bf, i);
if (j != -)
nums[nn++] = j;
}
bf[len+] = '\0';
for (i=; i<=len; ++i) {
k = j = ;
while (j<len) {
if (k != i) {
bf[k] = str[j];
++j;
}
++k;
}
for (ch='a'; ch<='z'; ++ch) {
bf[i] = ch;
j = find(bf, -);
if (j != -)
nums[nn++] = j;
}
}
} int comp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
} int main() {
int n = , f;
char buf[]; for (int i=; i<; ++i)
root.next[i] = NULL; while (scanf("%s", map[n])!=EOF && map[n][]!='#') {
create(map[n], n);
++n;
} while (scanf("%s", buf)!=EOF && buf[]!='#') {
f = find(buf, -);
if (f != -) {
printf("%s is correct\n", buf);
continue;
}
ffind(buf);
printf("%s:", buf);
if (nn) {
qsort(nums, nn, sizeof(int), comp);
for (int i = ; i<nn; ++i) {
if (i && nums[i] == nums[i-])
continue;
printf(" %s", map[nums[i]]);
}
}
printf("\n");
} return ;
}
【POJ】1035 Spell checker的更多相关文章
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 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 ...
- 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉
DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $ 当然这里的$i$和$k$都是偶数啦~ ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- 【POJ】1222 EXTENDED LIGHTS OUT
[算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...
随机推荐
- Qt 学习之路:Graphics View Framework
Graphics View 提供了一种接口,用于管理大量自定义的 2D 图形元素,并与之进行交互:还提供了用于将这些元素进行可视化显示的观察组件,并支持缩放和旋转.我们通常所说的 Linux 的 KD ...
- eslint error
暂时不想解决 报错: 3:16 error Component should be written as a pure function react/prefer-stateless-function ...
- CentOS安装最新的Mysql版本
Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖命令: # yum -y remove ...
- HTML5 文件域+FileReader 分段读取文件(四)
一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...
- .net 使用AjaxControlToolkit.dll 遇到的"Sys"未定义问题
1.配置文件一般都会缺少<httpHandlers></httpHandlers> 这一段, <httpHandlers> <remove verb=&quo ...
- ASP.NET Excel数据导入数据库
<identity impersonate="true"/> 是指模拟IIS身份验证 導入錯誤時可刪除 protected void btnImport_Click(o ...
- 请教如何实现UITextField值变化的实时监视
上网搜索以后发现基本的处理方法大概有三种1.KVO方式[textField addObserver:self forKeyPath:@"text" options:0 contex ...
- basicAnimation移动图形
目的:采用CABasicAnimation 点击屏幕上的点来是实现图像的位置移动 并且位置能够不反弹 难点:1 通过动画的KeyPath找到layer的属性 2 通过NSValue将点包装成对象 ...
- java 全角、半角字符串转换
转自:http://www.cnblogs.com/modou/articles/2679815.html 加入了空字符串的验证 半角转全角的方法: /** * @Title: ToSBC * ...
- VIM中文乱码
下面是关于VIM中文乱码问题的解决方法: 打开VIM的配制文件在里面加上一段这样的代码就可以了: set encoding=prc