【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 ...
随机推荐
- spring aop 如何切面到mvc 的controller--转载
原文:http://yjian84.iteye.com/blog/1920787 网上搜罗半天,不知道什么原因,看了源码,好像他们说的controller 是不受代理的,也对哈,不知道怎么办,于是在h ...
- 如何判断JDK是32位还是64位
第一种方法 在CMD窗口中使用java -version 命令进行查看 如果是64位的则会显示 Java HotSpot<TM>64-Bit 字样,32位的则没有类似信息. 注:这是Sun ...
- [转] Mac OX上安装MongoDb
https://scotch.io/tutorials/an-introduction-to-mongodb MongoDB的安装有好多种安装方法,有普通青年的HomeBrew方式,也有文艺青年的源码 ...
- linux-3.0下input_dev模型按键驱动
该代码在FL2440开发板上测试通过,为方便教学,将驱动中的platform_device和platform_driver故意分为两个驱动模块. [guowenxue@centos6 input_kb ...
- mailsend - Send mail via SMTP protocol from command line
Introduction mailsend is a simple command line program to send mail via SMTP protocol. I used to sen ...
- linux的openfire运行日志配置经历
openfire的日志可以通过/usr/openfire/lib/log4j.xml(与openfire的安装目录有关,我的openfire是安装在/usr/openfire/)的xml配置文件进行设 ...
- Bootstrap V3使用Tab标签
Bootstrap V3使用Tab标签 >>>>>>>>>>>>>>>>>>>> ...
- @ManyToMany中间表附加字段设计
在使用@ManyToMany时,若中间表只有相应的外键字段可以直接建立两个对应的Entity 设置ManyToMany @ManyToMany 两个表多对多关联 但若是中间表有自己的附加字段,这需要为 ...
- Android 开发笔记——通过 Intent 传递类对象
Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...
- Express难点解析
app.js 应用程序入口文件1.// view engine setup 设置视图引擎app.set('views', path.join(__dirname, 'views'));//告诉expr ...