I think this question is becoming more and more complicated. This question basically gives the framework of the English dictionary for the next lesson. It seems that the solution to this question is the so-called inverted index. A series of sentences are actually a series of words. Of course, some words should be removed. Then, index all the remaining words. Finally, output all sentences containing all words in Lexicographic Order. My practice is to define a structure index that contains a keyword and a pointer pointing to a linked list. each node in the linked list contains the position of the sentence where the keyword is located, and the position of the keyword in the sentence. Then, read the word. If it is not the word to be ignored, judge whether the word is already in the keyword table, and add it to the end of the linked list of the corresponding keyword, if not, add it to the keyword list. Sort the keywords, traverse the keyword list, and output the sentence where the keywords are located based on the index. In general, it is complicated, but as long as one module is implemented and one module is debugged, it can still be solved. Haha ~
Question:
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 shocould 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
Source code:
# Include <stdio. h> # include <string. h> # include <stdlib. h> typedef struct POS {// index, stores the sentence location where the keyword is located and its position in the sentence int row; // The title Position int cow; // position of the keyword in the title: struct POS * Next;} Pos; typedef struct index {// storage Keyword: Char word [55]; POS * head; // head pointer pointing to the keyword index linked list} index; index list [3000 + 5]; char ignored [55] [15]; // The word char title to be ignored [220] [20] [50]; // store the sentence int title_len [220]; // store each int ignore_num = 0; // Number of words to be ignored int title_num = 0; // Title Number of int key_num = 0; // Number of keywords void get_ignore (); // obtain the word void get_title () to be ignored; // obtain titleint ignore (char *); // determine whether a word needs to be ignored int exist (char *); // determine whether the keyword has been in the keyword table void convert (char *); void add_key (char *, int, INT); // Add the new keyword void add_element (INT, Int, INT); // Add the new index void sort () to the existing keyword; int main () {int I, j, k; int row, num, cow; POS * P; // freopen ("data", "r", stdin); get_ignore (); get_title (); sort (); // keyword sorting for (I = 0; I <key_num; I ++ ){ P = list [I]. head; while (p) {ROW = p-> row; cow = p-> cow; For (j = 0; j <title_len [row]; j ++) if (j = cow) printf ("% S % C", list [I]. word, j = title_len [row]-1? '\ N': ''); // output the uppercase keyword else printf (" % S % C ", title [row] [J], j = title_len [row]-1? '\ N': ''); P = p-> next;} return 0;} void get_ignore () {// obtain the word ignore_num to be ignored = 0; while (scanf ("% s", ignored [ignore_num ++]) & ignored [ignore_num-1] [0]! = ':'); Ignore_num --;} void get_title () {// obtain the title int cow_cnt = 0, Pos; char C; title_num = 0; title_len [0] = 0; while (~ Scanf ("% s", title [title_num] [cow_cnt]) {convert (title [title_num] [cow_cnt]); // convert all words in the title into lowercase if (! Ignore (title [title_num] [cow_cnt]) {// if it is not the word to be ignored (Pos = exist (title [title_num] [cow_cnt]) =-1) // It is not an existing keyword add_key (title [title_num] [cow_cnt], title_num, cow_cnt); else // it already exists in the existing keyword, add the index add_element (title_num, cow_cnt, POS);} If (C = getchar () = '\ n') {title_len [title_num] ++; title_num ++; title_len [title_num] = 0; cow_cnt = 0;} else {cow_cnt ++; title_len [title_num] ++ ;}} return ;} int ignore (char * Word) {// whether the word int I to be ignored; for (I = 0; I <ignore_num; I ++) if (strcmp (word, ignored [I]) = 0) return 1; return 0;} int exist (char * Word) {// whether char a [55]; int I; if (key_num = 0) Return-1; strcpy (A, word); for (I = 0; I <strlen (a); I ++) A [I] = toupper (A [I]); for (I = 0; I <key_num; I ++) if (strcmp (list [I]. word, A) = 0) return I; Return-1;} void convert (char * Word) {// convert it to the lowercase letter form int I; for (I = 0; I <strlen (Word); I ++) word [I] = tolower (word [I]);} void add_element (INT row, int cow, int POS) {pos * New, * P; new = (Pos *) malloc (sizeof (POS); New-> ROW = row; New-> cow = cow; P = list [POS]. head; while (p-> next) // Add the new index to the end of the index linked list. The question must be P = p-> next; new-> next = p-> next; P-> next = new;} void add_key (char * word, int row, int cow) {// Create a New Keyword POS * New; int I; strcpy (list [key_num]. word, word); New = (Pos *) malloc (sizeof (POS); New-> ROW = row; New-> cow = cow; for (I = 0; I <strlen (list [key_num]. word); I ++) list [key_num]. word [I] = toupper (list [key_num]. word [I]); list [key_num]. head = new; key_num ++;} void sort () {// sort the keyword table int I, j; index temp; for (I = 1; I <key_num; I ++) for (j = I; j> 0; j --) if (strcmp (list [J]. word, list [J-1]. word) <0) {temp = list [J]; list [J] = list [J-1]; list [J-1] = temp;} else break ;}