zoukankan      html  css  js  c++  java
  • Uva 123 Searching Quickly

    Searching Quickly 

    Time limit: 3.000 seconds

    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


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    #define MAXN 400
    char title[204][MAXN];
    char keyword[52][14]; 
    typedef struct charter{
        char character[MAXN]; //存储每个裁剪下来的单词 
        int rank; // 表示这个单词属于那行title 
        int start, end; // 这个单词在title中的位置 
    }charter;
    
    charter mes[3004];
    
    int dealenter(char *temp)
    {// 处理fgets函数的“不和谐”,同时在每个title后缀处加‘ ’以便处理 
        int len = strlen(temp);
        if(temp[len-1] == '\n') temp[len-1] = ' ';
        else temp[len++] = ' ', temp[len] = '\0';
        return 0;
    }
    
    
    int calculate(int n1, int n2, int n3)
    {// 对单词进行排序 
        int i, j, k, flag;
        charter temp;
        for(i=0; i<n3-1; ++i)
        {
            flag = 1;
            for(j=0; j<n3-1-i; ++j)
            {
                if(strcmp(mes[j].character, mes[j+1].character) > 0)
                {
                    flag = 0;
                    temp = mes[j], mes[j] = mes[j+1], mes[j+1] = temp;
                }
            }
            if(flag) break;
        }
        return 0;
    }
    
    int shuai(int n1, char *from)
    {// 判断单词是否需要被忽略,返回1表示应该被忽略 
        int i, j, flag = 0;
        for(i=0; i<n1; ++i)
        {
            if(strcmp(keyword[i], from) == 0) {flag = 1; break;}
        }
        
        return flag;
    }
    
    
    int lastline(int n1, int n2, int n3)
    {// 输出处理 
        int i, j, k, t, flag, len, start, end;
        for(i=0; i<n3; ++i)
        {
            if(!shuai(n1, mes[i].character))
            {
                j = mes[i].rank;
                start = mes[i].start, end = mes[i].end;
                for(t=0; t<start; ++t)
                printf("%c", title[j][t]);
                len = strlen(mes[i].character);
                for(k=0; k<len; ++k)
                    printf("%c", mes[i].character[k] - ('a' - 'A'));
                len = strlen(title[j]);
                for(k=end+1; k<len; ++k)
                   printf("%c", title[j][k]);
                   printf("\n");
            }
        }
    }
    
    int main()
    {
        int  i, j, t, k, n, m, count, cnt3, cnt1, cnt2, cnt4, flag, flag2, len, doc;
        char temp[MAXN];
        flag = cnt1 = cnt2 = cnt3 = 0;
        while(fgets(temp, MAXN, stdin) != NULL)
        {
            dealenter(temp);
            len = strlen(temp);
            if(strcmp(temp, ":: ") == 0) flag = 1;
            else if(!flag)
                {// 存储 ignore-words 
                    strcpy(keyword[cnt1], temp);
                    keyword[cnt1++][len-1] = '\0';
                }
            else
            {// 存储每行title的同时,将每个title中的单词进行分割,存储到结构体数组中 
                for(i=0,flag2=cnt4=0,doc=-1; i<len; ++i)
                {
                    
                    if(temp[i] != ' ')
                    {
                        if(doc == -1) doc = i;
                        if(isupper(temp[i])) mes[cnt3].character[cnt4++] = temp[i] = towlower(temp[i]);
                        else mes[cnt3].character[cnt4++] = temp[i];
                        flag2 = 1;
                    }
                    else if(flag2)
                    {
                        mes[cnt3].rank = cnt2, mes[cnt3].start = doc, mes[cnt3].end = i-1;
                        mes[cnt3].character[cnt4] = '\0';
                        cnt3++, cnt4 = 0;
                        flag2 = 0, doc = -1;
                    }
                }
                    strcpy(title[cnt2], temp);
                    title[cnt2++][len-1] = '\0';
            }
        }
        calculate(cnt1, cnt2, cnt3);
        lastline(cnt1, cnt2, cnt3);
        return 0;
    } 

    解题报告:

    Runtime Error X 2  [据其原因是最后忘了加 return 0 !!! 果然,有很长时间没做题了]

    看题目找思路,我的做法比较愚笨:

    首先需要将ignore-word 和 titles[换成lowcase后的character] 用数组存储起来

    然后在title中做文章,将每个单词从中分割出来,需要存储的信息是这个单词属于哪一title,并存储title中所在的位置[下标表示]   

    将取出的单词进行按字典顺序排序

    筛掉需要忽略掉的单词

    对每个剩下的单词,根据已存储的其他关于此单词的信息,找到所属的title,分三部分按题目要求样式输出

    测试数据提供:

    Sample Input:
    the
    for
    on
    in
    and
    is
    it
    american
    canadian
    great
    lakes
    ::
    The Niagara Falls are voluminous waterfalls on the Niagara River
    straddling the international border between the Canadian province
    of Ontario and the U S  state of New York  The falls are
    N miles BBBBBBSSSM kmBBBBBBSSS north northwest of Buffalo New York and
    S miles BBBBBBSSSZ kmBBBBBBSSS south southeast of Toronto Ontario
    between the twin cities of Niagara Falls Ontario
    and Niagara Falls New York  Niagara Falls is composed
    of two major sections separated by Goat Island:
    Horseshoe Falls the majority of which lies on the
    Canadian side of the border and American Falls on the
    American side  The smaller Bridal Veil Falls are also
    located on the American side separated from the main falls
    by Luna Island  Niagara Falls were formed when glaciers
    receded at the end of the Wisconsin glaciation BBBBBBSSSthe last ice ageBBBBBBSSS
    and water from the newly formed Great Lakes carved a path through
    the Niagara Escarpment en route to the Atlantic Ocean
    While not exceptionally high the Niagara Falls are very wide
    More than six million cubic feet BBBBBBSSSXYZKLM mSBBBBBBSSS of water falls
    over the crest line every minute in high flow and almost
    F million cubic feet BBBBBBSSSXYZKLM mSBBBBBBSSS on average  It is the most
    powerful waterfall in North America
    
    The Niagara Falls are renowned both for their beauty and
    as a valuable source of hydroelectric power  Managing the
    balance between recreational commercial and industrial uses
    has been a challenge for the stewards of the falls since the TKLMs
    
    Sample Output:
    
    and water from the newly formed great lakes carved A path through
    as A valuable source of hydroelectric power  managing the
    has been A challenge for the stewards of the falls since the tklms
    receded at the end of the wisconsin glaciation bbbbbbsssthe last ice AGEBBBBBBSSS
    over the crest line every minute in high flow and ALMOST
    american side  the smaller bridal veil falls are ALSO
    powerful waterfall in north AMERICA
    the niagara falls ARE voluminous waterfalls on the niagara river
    of ontario and the u s  state of new york  the falls ARE
    american side  the smaller bridal veil falls ARE also
    while not exceptionally high the niagara falls ARE very wide
    the niagara falls ARE renowned both for their beauty and
    AS a valuable source of hydroelectric power  managing the
    receded AT the end of the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
    the niagara escarpment en route to the ATLANTIC ocean
    f million cubic feet bbbbbbsssxyzklm msbbbbbbsss on AVERAGE  it is the most
    BALANCE between recreational commercial and industrial uses
    n miles BBBBBBSSSM kmbbbbbbsss north northwest of buffalo new york and
    receded at the end of the wisconsin glaciation BBBBBBSSSTHE last ice agebbbbbbsss
    more than six million cubic feet BBBBBBSSSXYZKLM msbbbbbbsss of water falls
    f million cubic feet BBBBBBSSSXYZKLM msbbbbbbsss on average  it is the most
    s miles BBBBBBSSSZ kmbbbbbbsss south southeast of toronto ontario
    the niagara falls are renowned both for their BEAUTY and
    has BEEN a challenge for the stewards of the falls since the tklms
    straddling the international border BETWEEN the canadian province
    BETWEEN the twin cities of niagara falls ontario
    balance BETWEEN recreational commercial and industrial uses
    straddling the international BORDER between the canadian province
    canadian side of the BORDER and american falls on the
    the niagara falls are renowned BOTH for their beauty and
    american side  the smaller BRIDAL veil falls are also
    n miles bbbbbbsssm kmbbbbbbsss north northwest of BUFFALO new york and
    of two major sections separated BY goat island:
    BY luna island  niagara falls were formed when glaciers
    and water from the newly formed great lakes CARVED a path through
    has been a CHALLENGE for the stewards of the falls since the tklms
    between the twin CITIES of niagara falls ontario
    balance between recreational COMMERCIAL and industrial uses
    and niagara falls new york  niagara falls is COMPOSED
    over the CREST line every minute in high flow and almost
    more than six million CUBIC feet bbbbbbsssxyzklm msbbbbbbsss of water falls
    f million CUBIC feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
    the niagara escarpment EN route to the atlantic ocean
    receded at the END of the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
    the niagara ESCARPMENT en route to the atlantic ocean
    over the crest line EVERY minute in high flow and almost
    while not EXCEPTIONALLY high the niagara falls are very wide
    F million cubic feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
    the niagara FALLS are voluminous waterfalls on the niagara river
    of ontario and the u s  state of new york  the FALLS are
    between the twin cities of niagara FALLS ontario
    and niagara FALLS new york  niagara falls is composed
    and niagara falls new york  niagara FALLS is composed
    horseshoe FALLS the majority of which lies on the
    canadian side of the border and american FALLS on the
    american side  the smaller bridal veil FALLS are also
    located on the american side separated from the main FALLS
    by luna island  niagara FALLS were formed when glaciers
    while not exceptionally high the niagara FALLS are very wide
    more than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water FALLS
    the niagara FALLS are renowned both for their beauty and
    has been a challenge for the stewards of the FALLS since the tklms
    more than six million cubic FEET bbbbbbsssxyzklm msbbbbbbsss of water falls
    f million cubic FEET bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
    over the crest line every minute in high FLOW and almost
    by luna island  niagara falls were FORMED when glaciers
    and water from the newly FORMED great lakes carved a path through
    located on the american side separated FROM the main falls
    and water FROM the newly formed great lakes carved a path through
    receded at the end of the wisconsin GLACIATION bbbbbbsssthe last ice agebbbbbbsss
    by luna island  niagara falls were formed when GLACIERS
    of two major sections separated by GOAT island:
    HAS been a challenge for the stewards of the falls since the tklms
    while not exceptionally HIGH the niagara falls are very wide
    over the crest line every minute in HIGH flow and almost
    HORSESHOE falls the majority of which lies on the
    as a valuable source of HYDROELECTRIC power  managing the
    receded at the end of the wisconsin glaciation bbbbbbsssthe last ICE agebbbbbbsss
    balance between recreational commercial and INDUSTRIAL uses
    straddling the INTERNATIONAL border between the canadian province
    by luna ISLAND  niagara falls were formed when glaciers
    of two major sections separated by goat ISLAND
    n miles bbbbbbsssm KMBBBBBBSSS north northwest of buffalo new york and
    s miles bbbbbbsssz KMBBBBBBSSS south southeast of toronto ontario
    receded at the end of the wisconsin glaciation bbbbbbsssthe LAST ice agebbbbbbsss
    horseshoe falls the majority of which LIES on the
    over the crest LINE every minute in high flow and almost
    LOCATED on the american side separated from the main falls
    by LUNA island  niagara falls were formed when glaciers
    located on the american side separated from the MAIN falls
    of two MAJOR sections separated by goat island:
    horseshoe falls the MAJORITY of which lies on the
    as a valuable source of hydroelectric power  MANAGING the
    n MILES bbbbbbsssm kmbbbbbbsss north northwest of buffalo new york and
    s MILES bbbbbbsssz kmbbbbbbsss south southeast of toronto ontario
    more than six MILLION cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
    f MILLION cubic feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the most
    over the crest line every MINUTE in high flow and almost
    MORE than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
    f million cubic feet bbbbbbsssxyzklm msbbbbbbsss on average  it is the MOST
    more than six million cubic feet bbbbbbsssxyzklm MSBBBBBBSSS of water falls
    f million cubic feet bbbbbbsssxyzklm MSBBBBBBSSS on average  it is the most
    N miles bbbbbbsssm kmbbbbbbsss north northwest of buffalo new york and
    of ontario and the u s  state of NEW york  the falls are
    n miles bbbbbbsssm kmbbbbbbsss north northwest of buffalo NEW york and
    and niagara falls NEW york  niagara falls is composed
    and water from the NEWLY formed great lakes carved a path through
    the NIAGARA falls are voluminous waterfalls on the niagara river
    the niagara falls are voluminous waterfalls on the NIAGARA river
    between the twin cities of NIAGARA falls ontario
    and NIAGARA falls new york  niagara falls is composed
    and niagara falls new york  NIAGARA falls is composed
    by luna island  NIAGARA falls were formed when glaciers
    the NIAGARA escarpment en route to the atlantic ocean
    while not exceptionally high the NIAGARA falls are very wide
    the NIAGARA falls are renowned both for their beauty and
    n miles bbbbbbsssm kmbbbbbbsss NORTH northwest of buffalo new york and
    powerful waterfall in NORTH america
    n miles bbbbbbsssm kmbbbbbbsss north NORTHWEST of buffalo new york and
    while NOT exceptionally high the niagara falls are very wide
    the niagara escarpment en route to the atlantic OCEAN
    OF ontario and the u s  state of new york  the falls are
    of ontario and the u s  state OF new york  the falls are
    n miles bbbbbbsssm kmbbbbbbsss north northwest OF buffalo new york and
    s miles bbbbbbsssz kmbbbbbbsss south southeast OF toronto ontario
    between the twin cities OF niagara falls ontario
    OF two major sections separated by goat island:
    horseshoe falls the majority OF which lies on the
    canadian side OF the border and american falls on the
    receded at the end OF the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
    more than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss OF water falls
    as a valuable source OF hydroelectric power  managing the
    has been a challenge for the stewards OF the falls since the tklms
    of ONTARIO and the u s  state of new york  the falls are
    s miles bbbbbbsssz kmbbbbbbsss south southeast of toronto ONTARIO
    between the twin cities of niagara falls ONTARIO
    OVER the crest line every minute in high flow and almost
    and water from the newly formed great lakes carved a PATH through
    as a valuable source of hydroelectric POWER  managing the
    POWERFUL waterfall in north america
    straddling the international border between the canadian PROVINCE
    RECEDED at the end of the wisconsin glaciation bbbbbbsssthe last ice agebbbbbbsss
    balance between RECREATIONAL commercial and industrial uses
    the niagara falls are RENOWNED both for their beauty and
    the niagara falls are voluminous waterfalls on the niagara RIVER
    the niagara escarpment en ROUTE to the atlantic ocean
    of ontario and the u S  state of new york  the falls are
    S miles bbbbbbsssz kmbbbbbbsss south southeast of toronto ontario
    of two major SECTIONS separated by goat island:
    of two major sections SEPARATED by goat island:
    located on the american side SEPARATED from the main falls
    canadian SIDE of the border and american falls on the
    american SIDE  the smaller bridal veil falls are also
    located on the american SIDE separated from the main falls
    has been a challenge for the stewards of the falls SINCE the tklms
    more than SIX million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
    american side  the SMALLER bridal veil falls are also
    as a valuable SOURCE of hydroelectric power  managing the
    s miles bbbbbbsssz kmbbbbbbsss SOUTH southeast of toronto ontario
    s miles bbbbbbsssz kmbbbbbbsss south SOUTHEAST of toronto ontario
    of ontario and the u s  STATE of new york  the falls are
    has been a challenge for the STEWARDS of the falls since the tklms
    STRADDLING the international border between the canadian province
    more THAN six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of water falls
    the niagara falls are renowned both for THEIR beauty and
    and water from the newly formed great lakes carved a path THROUGH
    has been a challenge for the stewards of the falls since the TKLMS
    the niagara escarpment en route TO the atlantic ocean
    s miles bbbbbbsssz kmbbbbbbsss south southeast of TORONTO ontario
    between the TWIN cities of niagara falls ontario
    of TWO major sections separated by goat island:
    of ontario and the U s  state of new york  the falls are
    balance between recreational commercial and industrial USES
    as a VALUABLE source of hydroelectric power  managing the
    american side  the smaller bridal VEIL falls are also
    while not exceptionally high the niagara falls are VERY wide
    the niagara falls are VOLUMINOUS waterfalls on the niagara river
    and WATER from the newly formed great lakes carved a path through
    more than six million cubic feet bbbbbbsssxyzklm msbbbbbbsss of WATER falls
    powerful WATERFALL in north america
    the niagara falls are voluminous WATERFALLS on the niagara river
    by luna island  niagara falls WERE formed when glaciers
    by luna island  niagara falls were formed WHEN glaciers
    horseshoe falls the majority of WHICH lies on the
    WHILE not exceptionally high the niagara falls are very wide
    while not exceptionally high the niagara falls are very WIDE
    receded at the end of the WISCONSIN glaciation bbbbbbsssthe last ice agebbbbbbsss
    of ontario and the u s  state of new YORK  the falls are
    n miles bbbbbbsssm kmbbbbbbsss north northwest of buffalo new YORK and
    and niagara falls new YORK  niagara falls is composed

  • 相关阅读:
    SELFJOIN
    lLinux编程大全
    一个基础但是隐晦的c++语法问题
    cocos2dx内存优化
    iOS和android游戏纹理优化和内存优化(cocos2dx)
    STL学习小结
    C++11
    游戏资源打包
    C++ '__FILE__' and '__LINE__
    Cocos2dx纹理优化的一些方案
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2850249.html
Copyright © 2011-2022 走看看