uva 123 Searching Quickly(遍曆+排序)

來源:互聯網
上載者:User

 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 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

題目大意:給出一些ingore word(可忽略單詞),再給出一些句子,句子由可忽略單詞和不可忽略單片語成, 要求找出所有的不可忽略單詞,輸出含不可忽略單詞的句子(此時不可忽略單詞要大寫),注意一個不可忽略單詞可能出現在多個句子裡,一個句子可能有多個(包括相同的)不可忽略單詞(輸出時按照不可忽略單詞的字典序,相同單詞按照句子的順序)

解題思路:讀入不可忽略單詞,再讀入句子,每讀入一個句子時將句子分解成單詞,分解的同時判斷它是否為不可忽略單詞(與前面可忽略單詞進行比較),讀完所有的句子以後對不可忽略單詞進行排序,然後再對每個不可忽略單詞尋找每條語句。

PS:題目本身沒有難度,但是要細心,因為比較繁瑣。

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 10005#define T 205#define M 20struct say{char word[M];}s[T];struct talk{char sen[T][M];int cnt;}title[T];int n_s = 0, n_title = 0, n_ignore = 0;;char ignore_word[T][M];void change(char str[]){int len = strlen(str);for (int i = 0; i < len; i++){if(str[i] >= 'A' && str[i] <= 'Z')str[i] += 32;}}int cmp(const say a, const say b){return strcmp(a.word, b.word) < 0;}void print(talk t, int k){for (int i = 0; i < t.cnt - 1; i++){if (i == k){for (int j = 0; j < strlen(t.sen[i]); j++)printf("%c", t.sen[i][j] - 32);printf(" ");}else printf("%s ", t.sen[i]);}if (t.cnt - 1 == k){for (int j = 0; j < strlen(t.sen[t.cnt - 1]); j++)printf("%c", t.sen[t.cnt - 1][j] -32);printf("\n");}elseprintf("%s\n", t.sen[t.cnt - 1]);}void find(talk t, say a){for (int i = 0; i < t.cnt; i++){if (strcmp(t.sen[i], a.word) == 0)print(t, i);}}void judge(char str[]){for (int i = 0; i < n_ignore; i++){if (strcmp(ignore_word[i], str) == 0)return;}for (int i = 0; i < n_s; i++){if (strcmp(s[i].word, str) == 0)return;}strcpy(s[n_s++].word, str);}void build(char str[], int k){int len = strlen(str), n = 0, m = 0;for (int i = 0; i < len; i++){if (str[i] >= 'a' && str[i] <= 'z')title[k].sen[n][m++] = str[i];else{title[k].sen[n][m] = '\0';judge(title[k].sen[n]);m = 0;n++;}}title[k].sen[n++][m] = '\0';judge(title[k].sen[n - 1]);title[k].cnt = n;}int main(){char name[N];// Read.while (1){gets(ignore_word[n_ignore]);if (strcmp(ignore_word[n_ignore], "::") == 0)break;change(ignore_word[n_ignore]);n_ignore++;}while (gets(name) != NULL){change(name);build(name, n_title);n_title++;}// Ready.sort(s, s + n_s, cmp);// Find.for (int i = 0; i < n_s; i++)for (int j = 0; j < n_title; j++)find(title[j], s[i]);return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.