Ultraviolet A 123 searching quickly (traversal + sorting)

Source: Internet
Author: 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:

                      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 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 shoshould 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 shoshould appear in all upper-case letters. all other words in a title shoshould be in lower-case letters. titles in the kwic-index with the same keyword shoshould 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 shoshould 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

Some ingore words (word can be ignored) are given, and some sentences are given. Sentences are composed of words that can be ignored and words that cannot be ignored. All words that cannot be ignored must be found, output Sentences containing words that cannot be ignored (uppercase words are not ignored at this time). Note that a word that cannot be ignored may appear in multiple sentences, and a sentence may have multiple (including the same) words) words cannot be ignored (output is in the Lexicographic Order of words that cannot be ignored, and the same words are in the sentence order)

Solution: when reading a sentence, you cannot ignore words and then read sentences. Each time you read a sentence, the sentence is broken down into words, when splitting, you can determine whether it is a word that cannot be ignored (compared with the previous word that can be ignored). After reading all the sentences, you can sort words that cannot be ignored, then, you can find each statement for each word that cannot be ignored.

PS: the question itself is not difficult, but be careful, because it is complicated.

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.