字典树。

 #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的更多相关文章

  1. 【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, ...

  2. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  3. poj 1035 Spell checker ( 字符串处理 )

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16675   Accepted: 6087 De ...

  4. poj 1035 Spell checker

    Spell checker Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   J ...

  5. [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 De ...

  6. 【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$都是偶数啦~ ...

  7. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  8. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  9. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

随机推荐

  1. Java——(五)Collection之List集合、ArrayList和Vector实现类

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 一.List集合 List集合代表一个元素有序.客重复的集合,集合中每个元素都有其对应的顺序索引 ...

  2. swift 关于delegate

    Cocoa 开发中接口-委托 (protocol-delegate) 模式是一种常用的设计模式,它贯穿于整个 Cocoa 框架中,为代码之间的关系清理和解耦合做出了不可磨灭的贡献. 在 ARC 中,对 ...

  3. PHP 5.6启动失败failed to open configuration file '/usr/local/php/etc/php-fpm.conf'

    PHP编译安装完毕,启动失败,提示 [-Jun- ::] ERROR: failed to open configuration ) [-Jun- ::] ERROR: failed to load ...

  4. PowerDesigner15的安装和破解

    一.PowerDesigner15的安装 运行安装包,出现如下安装界面

  5. MD5 SHA-1 示例

    测试代码 public class SignatureUtils {     public static void main(String[] args) throws Exception {     ...

  6. AS【常用插件】

    安装插件,Settings -->[Plugins]-->搜索-->点击install-->重启AS 禁用插件,右侧面板会显示出已经安装的插件列表,取消勾选即可禁用插件 AS插 ...

  7. 利用MutationObserver对页面元素的改变进行监听

    'use strict'; let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || win ...

  8. if elsif;报错;new赋值

    1. IF INSERTING THEN          BEGIN 中间不能为空          END;ELSIF DELETING THEN         BEGIN          E ...

  9. 收集 数据库的awr数据,生成报告

    该脚本只是把awr报告的内容,原封不动的 展现出来,做记录 awrreport.sql  脚本内容如下: *********************************************** ...

  10. Linux mail 命令使用

    linux mail 命令参数: 使用mail发邮件时,必须先将sendmail服务启动. mail –s “邮件主题” –c”抄送地址” –b “密送地址” -- -f 发送人邮件地址 –F 发件人 ...