• UVA 123 Searching Quickly 开始新的路程


    搞了一个寒假ACM了 可是一到校内月赛就感觉自己学的基本用不上 ,又一次陷入了这种大神说这题水,我看这题根本没思路的折磨中。

    请教了好几个校内的学长 ,坚定了我要继续搞下去的信心 。我准备每天至少刷两道题 每道题都会发到这个blog上 。自己只要坚持下去 每道题都有收获 就足矣。

    感慨完了 开始正文 

     Searching Quickly 

    Background

    Searching and sorting are part of the theory and practice of computer science. For example, binary search provides a good example of an easy-to-understand algorithm with sub-linear complexity. Quicksort is an efficient tex2html_wrap_inline29 [average case] comparison based sort.

    KWIC-indexing is an indexing method that permits efficient ``human search'' of, for example, a list of titles.

    The Problem

    Given a list of titles and a list of ``words to ignore'', you are to write a program that generates a KWIC (Key Word In Context) index of the titles. In a KWIC-index, a title is listed once for each keyword that occurs in the title. The KWIC-index is alphabetized by keyword.

    Any word that is not one of the ``words to ignore'' is a potential keyword.

    For example, if words to ignore are ``the, of, and, as, a'' and the list of titles is:

    Descent of Man
    The Ascent of Man
    The Old Man and The Sea
    A Portrait of The Artist As a Young Man

    A KWIC-index of these titles might be given by:

                          a portrait of the ARTIST as a young man 
                                        the ASCENT of man 
                                            DESCENT of man 
                                 descent of MAN 
                              the ascent of MAN 
                                    the old MAN and the sea 
        a portrait of the artist as a young MAN 
                                        the OLD man and the sea 
                                          a PORTRAIT of the artist as a young man 
                        the old man and the SEA 
              a portrait of the artist as a YOUNG man

    The Input

    The input is a sequence of lines, the string :: is used to separate the list of words to ignore from the list of titles. Each of the words to ignore appears in lower-case letters on a line by itself and is no more than 10 characters in length. Each title appears on a line by itself and may consist of mixed-case (upper and lower) letters. Words in a title are separated by whitespace. No title contains more than 15 words.

    There will be no more than 50 words to ignore, no more than than 200 titles, and no more than 10,000 characters in the titles and words to ignore combined. No characters other than 'a'-'z', 'A'-'Z', and white space will appear in the input.

    The Output

    The output should be a KWIC-index of the titles, with each title appearing once for each keyword in the title, and with the KWIC-index alphabetized by keyword. If a word appears more than once in a title, each instance is a potential keyword.

    The keyword should appear in all upper-case letters. All other words in a title should be in lower-case letters. Titles in the KWIC-index with the same keyword should appear in the same order as they appeared in the input file. In the case where multiple instances of a word are keywords in the same title, the keywords should be capitalized in left-to-right order.

    Case (upper or lower) is irrelevant when determining if a word is to be ignored.

    The titles in the KWIC-index need NOT be justified or aligned by keyword, all titles may be listed left-justified.

    Sample Input

    is
    the
    of
    and
    as
    a
    but
    ::
    Descent of Man
    The Ascent of Man
    The Old Man and The Sea
    A Portrait of The Artist As a Young Man
    A Man is a Man but Bubblesort IS A DOG

    Sample Output

    a portrait of the ARTIST as a young man 
    the ASCENT of man 
    a man is a man but BUBBLESORT is a dog 
    DESCENT of man 
    a man is a man but bubblesort is a DOG 
    descent of MAN 
    the ascent of MAN 
    the old MAN and the sea 
    a portrait of the artist as a young MAN 
    a MAN is a man but bubblesort is a dog 
    a man is a MAN but bubblesort is a dog 
    the OLD man and the sea 
    a PORTRAIT of the artist as a young man 
    the old man and the SEA 
    a portrait of the artist as a YOUNG man

    学习了水果的博客 按照其思路写出了代码。。multimap的自动排序真的很方便 ,String用起来也比char[]方便多了。STL还需要多学学
    代码如下
     1 #include<cstdio>
     2 #include<string>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<set>
     6 #include<map>
     7 #include<cctype>
     8 #define MAX 220
     9 using namespace std;
    10 
    11 int main(void)
    12 {
    13     set<string> ig;
    14     string igs;                            //忽略字串
    15     string title;                        //标题字串
    16     multimap<string, string> res;        //multimap本身就是一个排好序的关联容器 很方便 而且正好符合这道题的两个要求
    17     while (cin >> igs && igs != "::")
    18         ig.insert(igs);
    19     getchar();                            //吃回车 
    20     while (getline(cin, title))
    21     {
    22         int len = title.size();
    23         for (int i = 0; i < len; i++)
    24             title[i] = tolower(title[i]);
    25         for (int i = 0; i < len; i++)
    26         {
    27             if (!isalpha(title[i])) continue;
    28             string key;                                //记录关键词的字符串-- key
    29             int current_pos = i;                    //记录当前位置 好便于下面的替换 把关键词替换为大写
    30             while (i < title.size() && isalpha(title[i]))
    31                 key += title[i++];                    //把关键词copy到key 这时就体现了String类的方便
    32             if (!ig.count(key))
    33             {
    34                 int len0 = key.size();
    35                 for (int j = 0; j < len0; j++)
    36                     key[j] = toupper(key[j]);
    37                 string tmp = title;
    38                 tmp.replace(current_pos, key.size(), key);                    //这句由于不懂replace看了半天 这句就是把小写的关键词replace成大写的
    39                 res.insert(make_pair(key,tmp));                                //因为multimap的元素是pair 所以插入要用 make_pair
    40             }
    41         }
    42     }
    43     for (multimap<string, string>::iterator i = res.begin(); i != res.end(); i++)
    44         cout << i->second << endl;
    45     //getchar();
    46     return 0;
    47 }
  • 相关阅读:
    倒排索引压缩
    记一次java内存溢出的解决过程
    [译]ES读写文档时shard-replication模型
    [转载]抓包工具Charles乱码解决办法
    Mac 快捷键整理(不定期更新)
    高效能人士执行的四原则(2017-12-15)
    scala sbt 添加国内镜像
    maven工程小红叉处理方法
    系统管理中 bash shell 脚本常用方法总结
    scala 2.11报错error: not found: type Application
  • 原文地址:https://www.cnblogs.com/VOID-133/p/VOIDUVA123.html
Copyright © 2020-2023  润新知