您的位置:首页 > 产品设计 > UI/UE

UVA 123 - Searching Quickly

2013-01-20 13:28 871 查看

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

[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


AC代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define WORDSNUM 16
#define MAXLEN 10005
#define TITLESNUM 202
#define IGNORESNUM 52
#define IGNORELEN 12
#define KEYWORDLEN 50
#define KEYWORDNUM 1000
char longch[MAXLEN];
char longch2[MAXLEN];

char * mystrcpy(char *ch1, char *ch2, int begini, int endi)
{
    for(int i = 0; i < endi-begini+1; i++)
    {
        ch1[i] = ch2[begini+i];
        if(i == endi-begini) ch1[i+1] = '\0';
    }
    return ch1;
}
struct ignoredictionary
{
    int count;
    char words[IGNORESNUM][IGNORELEN];
    bool exist(char *ch)
    {
        for(int i = 0; i < count; i++)
        {
            if(strcmp(words[i], ch)) continue;
            return true;
        }
        return false;
    }
} igdic;
struct Position
{
    int b, e;
};
struct Keywords
{
    int count;
    char kws[KEYWORDNUM][KEYWORDLEN];
    bool exist(char *ch)
    {
        for(int i = 0; i < count; i++)
        {
            if(strcmp(kws[i], ch)) continue;
            return true;
        }
        return false;
    }
    void add(char * ch)
    {
        if(!exist(ch))
        {
            strcpy(kws[count], ch);
            count++;
        }
    }
}keywords;
struct Title
{
    int count;
    bool key[WORDSNUM];
    Position psn[WORDSNUM];
    char content[MAXLEN];
    void format()
    {
        int begin = 0, end = 0;
        while(content[begin] != '\0')
        {
            if(content[begin] != ' ')
            {
                end = begin;
                while(content[end] != ' ' && content[end] != '\0')
                {
                    end++;
                }
                psn[count].b = begin;
                psn[count].e = end-1;
                for(int i = psn[count].b; i <= psn[count].e; i++)
                {
                    if(isupper(content[i]))
                    content[i] = tolower(content[i]);
                }
                mystrcpy(longch2, content, psn[count].b, psn[count].e);
                if(igdic.exist(longch2))
                {
                    key[count] = false;
                }
                else
                {
                    key[count] = true;
                    keywords.add(longch2);
                }
                count++;
                if(content[end] == '\0') break;
                begin = end;
            }
            begin++;
        }
    }
};
struct Titles
{
    int count;
    Title titles[TITLESNUM];
    void add(char * ch)
    {
        strcpy(titles[count].content, ch);
        titles[count].format();
        count++;
        return ;
    }
} a;
int cmp(const void * a, const void * b)
{
    return strcmp((char*)a,(char*)b);
}

void output()
{
    for(int i = 0; i < keywords.count; i++)
    {
        for(int j = 0; j < a.count; j++)
        {
            /*对第j个标题开始检查*/
            for(int k = 0; k < a.titles[j].count; k++)
            {
                /*对第k个单词进行检查*/
                if(a.titles[j].key[k])
                {
                    if( !strcmp(mystrcpy(longch2,
                                         a.titles[j].content,
                                         a.titles[j].psn[k].b,
                                         a.titles[j].psn[k].e)
                        , keywords.kws[i]) )
                    {
                        for(int b = a.titles[j].psn[k].b; b <= a.titles[j].psn[k].e; b++)
                        {
                            a.titles[j].content[b] = toupper(a.titles[j].content[b]);
                        }
                        printf("%s\n", a.titles[j].content);
                        for(int b = a.titles[j].psn[k].b; b <= a.titles[j].psn[k].e; b++)
                        {
                            a.titles[j].content[b] = tolower(a.titles[j].content[b]);
                        }
                    }
                }
            }
        }
    }

}
int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    igdic.count = 0;
    memset(igdic.words, 0, sizeof(igdic.words));
    a.count = 0;
    memset(a.titles, 0, sizeof(a.titles));
    keywords.count = 0;
    memset(keywords.kws, 0, sizeof(keywords.kws));
    char ch[IGNORELEN];
    int i = 0;
    for(i = 0; i < IGNORESNUM; i++)
    {
        gets(ch);
        if(!strcmp(ch, "::")) break;
        else
        {
            strcpy(igdic.words[i], ch);
        }
    }
    igdic.count = i;
    qsort(igdic.words, igdic.count, IGNORELEN * sizeof(char), cmp);
    while(gets(longch))
    {
        a.add(longch);
    }
    qsort(keywords.kws, keywords.count, sizeof(keywords.kws[0]), cmp);
    output();
    return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: