Searching Quickly

来源:互联网 发布:张成泽犬决 知乎 编辑:程序博客网 时间:2024/05/01 01:59


Searching Quickly


Description

Download as PDF

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 efficienttex2html_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 ManThe Ascent of ManThe Old Man and The SeaA 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

istheofandasabut::Descent of ManThe Ascent of ManThe Old Man and The SeaA Portrait of The Artist As a Young ManA 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>char word[55][15];char title[205][300];char temp[1000][15];int separte(int n, int m){int count = 0;char t[15];for (int i = 0; i < n; i++){for (int j = 0, k = 0;j <= strlen(title[i]); j++, k++){if (title[i][j] == ' ' || j == strlen(title[i])){t[k] = '\0';k = -1;bool f1 = true;bool f2 = true;for (int l = 0; l < count; l++)if (!strcmp(temp[l], t)){f1 = false;continue;}for (int l = 0; l < m; l++)if (!strcmp(word[l], t)){f1 = false;continue;}if (f1 && f2){strcpy(temp[count], t);count++;}continue;}t[k] = title[i][j];}}for (int i = 0; i < count - 1; i++){for (int j = i + 1; j < count; j++)if (strcmp(temp[i], temp[j]) > 0){char y[15];strcpy(y, temp[i]);strcpy(temp[i], temp[j]);strcpy(temp[j], y);}}return count;}void low(int n){for (int i = 0; i < n; i++) {for (int j = 0;; j++) {if (title[i][j] == '\0')break;else {if (title[i][j] >= 'A' && title[i][j] <= 'Z')title[i][j] += 32;}}}}void match(int m, int n, int allnum) {int num = 0;char x[15];while (num < allnum) {for (int i = 0; i < n; i++) {for (int j = 0, k = 0; j <= strlen(title[i]); j++, k++) {if (title[i][j] == ' ' || j == strlen(title[i])){x[k] = '\0';k = -1;if (!strcmp(x, temp[num])){for (int g = 0; g < j - strlen(x); g++)printf("%c", title[i][g]);for (int g = 0; g < strlen(x); g++)printf("%c", x[g] - 32);for (int g = j; g < strlen(title[i]); g++)printf("%c", title[i][g]);printf("\n");}continue;}x[k] = title[i][j];}}num++;}}int main(){int m, n;m = n = 0;while (scanf("%s", word[m]) && strcmp(word[m], "::")) { m++; }char c = getchar();while ((c = getchar()) != EOF) {int i = 0;title[n][i] = c;i++;while ((c = getchar()) != '\n') {title[n][i] = c;i++;}title[n][i] = '\0';n++;}low(n);int allnum = separte(n, m);match(m, n, allnum);}

0 0