zoukankan      html  css  js  c++  java
  • POJ1035 Spell checker

    题目来源:http://poj.org/problem?id=1035

    题目大意:

      写一个拼写检查程序。给定一些字典里的单词和一些待检查的词。需要检查的词可能是以下几种情况:

      1. 是正确的词;

      2. 删除一个字母可以成为一个正确的词;

      3. 加入一个字母可以成为一个正确的词;

      4. 替换一个字母可以成为正确的词。

      写一个程序找出给定的词可能是字典中的哪个词。

    输入:首先给出字典的所有单词。"#"结束。然后给出待查的词,"#"结束。

    输出:首先输出给出的待查词,若词正确输出" is correct",否则,先输出一个冒号,然后输出可能对应的字典里的词,有多个时按在字典中出现的顺序输出。每个单词前有个空格隔开。


    Sample Input

    i
    is
    has
    have
    be
    my
    more
    contest
    me
    too
    if
    award
    #
    me
    aware
    m
    contest
    hav
    oo
    or
    i
    fi
    mre
    #

    Sample Output

    me is correct
    aware: award
    m: i my me
    contest is correct
    hav: has have
    oo: too
    or:
    i is correct
    fi: i
    mre: more me

    遍历暴力解决之..(对于长度明显无法对应的词略去不查)。

     1 //////////////////////////////////////////////////////////////////////////
     2 //        POJ1035 Spell checker
     3 //        Memory: 964K        Time: 313MS
     4 //        Language: C++        Result: Accepted
     5 //////////////////////////////////////////////////////////////////////////
     6 
     7 #include <iostream>
     8 #include <map>
     9 #include <set>
    10 #include <list>
    11 #include <string>
    12 
    13 using namespace std;
    14 
    15 map<string, int> dictionary;
    16 set<size_t> lens;
    17 bool comp(string a, string b) {
    18     return (dictionary[a] - dictionary[b]) < 0 ? true : false;
    19 }
    20 
    21 int main() {
    22     string buff;
    23     int index = 0;
    24     while (cin >> buff && buff != "#") {
    25         dictionary[buff] = ++index;
    26         size_t length = buff.size();
    27         lens.insert(length);
    28     }
    29     while (cin >> buff && buff != "#") {
    30         size_t length = buff.size();
    31         list<string> queue;
    32         if (lens.find(length) != lens.end()) {
    33             if (dictionary.find(buff) != dictionary.end()) {
    34                 cout << buff << " is correct" << endl;
    35                 continue;
    36             }
    37             for (char c = 'a'; c <= 'z'; ++c) {
    38                 for (int i = 0; i < buff.size(); ++i) {
    39                     string temp = buff;
    40                     temp.replace(i, 1, 1, c);
    41                     if (dictionary.find(temp) != dictionary.end()) {
    42                         queue.push_back(temp);
    43                     }
    44                 }
    45             }
    46         }
    47         if (lens.find(length - 1) != lens.end()) {
    48             for (int i = 0; i < buff.size(); ++i) {
    49                 string temp = buff;
    50                 temp.erase(i, 1);
    51                 if (dictionary.find(temp) != dictionary.end()) {
    52                     queue.push_back(temp);
    53                 }
    54             }
    55         }
    56         if (lens.find(length + 1) != lens.end()) {
    57             for (char c = 'a'; c <= 'z'; ++c) {
    58                 for (int i = 0; i <= buff.size(); ++i) {
    59                     string temp = buff;
    60                     temp.insert(i, 1, c);
    61                     if (dictionary.find(temp) != dictionary.end()) {
    62                         queue.push_back(temp);
    63                     }
    64                 }
    65             }
    66         }
    67         queue.unique();
    68         queue.sort(comp);
    69         cout << buff << ":";
    70         for (list<string>::iterator it = queue.begin(); it != queue.end(); ++it) {
    71             cout << " " << *it;
    72         }
    73         cout << endl;
    74     }
    75     system("pause");
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    glog的编译和使用
    FFMPEG+SDL实现视频播放器
    SDL2学习(二):常用枚举值和函数
    SDL2学习(一): 显示一张图片
    2. chromium开发工具--gclient
    C# 获取时间大全
    用jquery-table2excel,进行导出excel
    SQL Server FOR XML PATH 和 STUFF函数的用法
    关于My Sql update语句不能用子查询的解决办法
    echarts呈现数据表图形
  • 原文地址:https://www.cnblogs.com/dengeven/p/3230459.html
Copyright © 2011-2022 走看看