【POJ】1035 Spell checker

字典树。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <vector>
  6 #include <string>
  7 using namespace std;
  8
  9 typedef struct Trie {
 10     int in;
 11     Trie *next[26];
 12 } Trie;
 13
 14 Trie root;
 15 char map[10005][25];
 16 int nums[205], nn;
 17
 18 void create(char str[], int in) {
 19     int i = 0, j, id;
 20     Trie *p = &root, *q;
 21
 22     while (str[i]) {
 23         id = str[i] - ‘a‘;
 24         ++i;
 25         if (p->next[id] == NULL) {
 26             q = (Trie *)malloc(sizeof(Trie));
 27             q->in = -1;
 28             for (j=0; j<26; ++j)
 29                 q->next[j] = NULL;
 30             p->next[id] = q;
 31         }
 32         p = p->next[id];
 33     }
 34     p->in = in;
 35 }
 36
 37 int find(char str[], int x) {
 38     int i = 0, id;
 39     Trie *p = &root;
 40
 41     while (str[i]) {
 42         if (i == x) {
 43             ++i;
 44             continue;
 45         }
 46         id = str[i] - ‘a‘;
 47         ++i;
 48         if (p->next[id] == NULL)
 49             return -1;
 50         p = p->next[id];
 51     }
 52
 53     return p->in;
 54 }
 55
 56 void ffind(char str[]) {
 57     int len = strlen(str), i, j, k;
 58     char ch, bk, bf[25];
 59     nn = 0;
 60
 61     for (i=0; i<=len; ++i)
 62         bf[i] = str[i];
 63     for (i=0; i<len; ++i) {
 64         bk = bf[i];
 65         for (ch=‘a‘; ch<=‘z‘; ++ch) {
 66             if (ch == bk)
 67                 continue;
 68             bf[i] = ch;
 69             j = find(bf, -1);
 70             if (j != -1)
 71                 nums[nn++] = j;
 72         }
 73         bf[i] = bk;
 74     }
 75
 76     for (i=0; i<len; ++i) {
 77         j = find(bf, i);
 78         if (j != -1)
 79             nums[nn++] = j;
 80     }
 81     bf[len+1] = ‘\0‘;
 82     for (i=0; i<=len; ++i) {
 83         k = j = 0;
 84         while (j<len) {
 85             if (k != i) {
 86                 bf[k] = str[j];
 87                 ++j;
 88             }
 89             ++k;
 90         }
 91         for (ch=‘a‘; ch<=‘z‘; ++ch) {
 92             bf[i] = ch;
 93             j = find(bf, -1);
 94             if (j != -1)
 95                 nums[nn++] = j;
 96         }
 97     }
 98 }
 99
100 int comp(const void *a, const void *b) {
101     return *(int *)a - *(int *)b;
102 }
103
104 int main() {
105     int n = 0, f;
106     char buf[25];
107
108     for (int i=0; i<26; ++i)
109         root.next[i] = NULL;
110
111     while (scanf("%s", map[n])!=EOF && map[n][0]!=‘#‘) {
112         create(map[n], n);
113         ++n;
114     }
115
116     while (scanf("%s", buf)!=EOF && buf[0]!=‘#‘) {
117         f = find(buf, -1);
118         if (f != -1) {
119             printf("%s is correct\n", buf);
120             continue;
121         }
122         ffind(buf);
123         printf("%s:", buf);
124         if (nn) {
125             qsort(nums, nn, sizeof(int), comp);
126             for (int i = 0; i<nn; ++i) {
127                 if (i && nums[i] == nums[i-1])
128                     continue;
129                 printf(" %s", map[nums[i]]);
130             }
131         }
132         printf("\n");
133     }
134
135     return 0;
136 }

【POJ】1035 Spell checker

时间: 2024-11-02 15:45:55

【POJ】1035 Spell checker的相关文章

[ACM] POJ 1035 Spell checker (单词查找,删除替换增加任何一个字母)

Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given word

POJ 1035 Spell checker (串)

题目大意: 问你后面输入的串能不能通过  加减一个字符,或者替换一个字符变成字典中的串. 思路分析: 直接模拟替换加减的过程. 比较两个串的长度.要相差为1 的时候才能进行模拟. 模拟的过程就是进行一个个的匹配. 发现失配的次数小于等于 1就可以输出. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <string> #i

【POJ】2278 DNA Sequence

各种wa后,各种TLE.注意若AC非法,则ACT等一定非法.而且尽量少MOD. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 105 8 #define NXTN 4 9 10 char str[15]; 11 12 typedef struct Matrix {

【POJ】1739 Tony&#39;s Tour

http://poj.org/problem?id=1739 题意:n×m的棋盘,'#'是障碍,'.'是空白,求左下角走到右下角且走过所有空白格子的方案数.(n,m<=8) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; #define BIT(a,b) ((a)<<((b)<<1)) #

【POJ】2449 Remmarguts&#39; Date(k短路)

http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k 首先我们建反向边,跑一次从汇到源的最短路,将跑出来的最短路作为估价函数h 根据f=g+h 我们将源s先走,此时实际价值g为0,估价为最短路(他们的和就是s-t的最短路) 将所有s所连的边都做相同的处理,加入到堆中(假设此时到达的点为x,那么x的g等于s到这个点的边权,因为根据最优,g+h此时是从x

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

【POJ】1056 IMMEDIATE DECODABILITY

字典树水题. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 typedef struct Trie { 6 bool v; 7 Trie *next[2]; 8 } Trie; 9 10 Trie *root; 11 12 bool create(char str[]) { 13 int i = 0, id; 14 bool ret = false; 15 Trie *p = root

【POJ】2418 Hardwood Species

简单字典树. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 128 6 7 typedef struct Trie { 8 int count; 9 Trie *next[MAXN]; 10 Trie() { 11 count = 0; 12 for (int i=0; i<MAXN; ++i) 13 next[i] = NULL; 14 } 15 }