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
The question is not hard to get bored. Give a group of words that can be ignored, and then give you a group of sentences. Except for the words that can be ignored, the remaining keywords are keywords. Each time a keyword is output in Lexicographic Order.
I wanted to arrange multiple keywords quickly, but I was not familiar with the old mistakes. After all the errors were completed, I submitted the runtime error for countless times and thought it was a small array opening, and again it was wrong.
Today, Baidu uses Getline when reading sentences, while gets seems to have missed it, so I changed it to fgets ....... pitfall: more than 30 runtime errors
# Include <stdio. h>
# Include <string. h>
Char ignore [51] [20], title [210] [1000], temp [30];
Struct s
{Char word [30];
Int postion, start;
} Key [2, 4000];
Void sort (int l, int R)
{Int I = L, j = r, x = Key [l]. postion, y = Key [l]. Start;
Char ch [30];
If (L> = r) return;
Strcpy (CH, key [l]. Word );
While (I <j)
{While (I <J & (strcmp (Key [J]. Word, CH)> = 0) -- J;
Strcpy (Key [I]. Word, key [J]. Word );
Key [I]. postion = Key [J]. postion;
Key [I]. Start = Key [J]. Start;
While (I <J & (strcmp (Key [I]. Word, CH) <= 0) + + I;
Strcpy (Key [J]. Word, key [I]. Word );
Key [J]. postion = Key [I]. postion;
Key [J]. Start = Key [I]. Start;
}
Strcpy (Key [I]. Word, CH );
Key [I]. postion = X;
Key [I]. Start = y;
Sort (L, I-1 );
Sort (I + 1, R );
}
Void main ()
{Int n = 1, m = 1, I, j, l, l, K, F, sum = 0, POs, begin;
While (scanf ("% s", ignore [N])
If (strcmp (ignore [N], ":") = 0) break; else ++ N;
Getchar ();
While (fgets (title [m], 1000, stdin ))
{L = strlen (title [m]);
Title [m] [l] = '';
Title [m] L-1] = '\ 0 ';
-- L;
K = 0;
For (I = 0; I <= L; I ++)
{
If (title [m] [I] = '') | (title [m] [I] = '\ 0 '))
{Temp [k] = '\ 0'; k = 0;
F = 1;
For (j = 1; j <n; j ++)
If (strcmp (temp, ignore [J]) = 0) {f = 0; break ;}
If (f) {++ sum; strcpy (Key [Sum]. Word, temp); Key [Sum]. postion = m; key [Sum]. Start = begin ;}
}
Else {If (k = 0) Begin = I; temp [k] = title [m] [I]; if (title [m] [I] <= 'Z') {temp [k] + = 32; title [m] [I] + = 32 ;}; + + k ;}
}
++ M;
}
Sort (1, sum );
For (k = 1; k <= sum; k ++)
{Pos = K;
While (j <sum & (strcmp (Key [K]. Word, key [POS + 1]. Word) = 0) + + Pos;
For (I = K; I <Pos; I ++)
For (j = I + 1; j <= Pos; j ++)
If (Key [I]. postion> key [J]. postion) | (Key [I]. postion = Key [J]. postion) & (Key [I]. start> key [J]. start ))
{F = Key [I]. postion; key [I]. postion = Key [J]. postion; key [J]. postion = F;
F = Key [I]. Start; key [I]. Start = Key [J]. Start; key [J]. Start = F;
}
}
For (I = 1; I <= sum; I ++)
{K = Key [I]. postion;
L = strlen (Key [I]. Word); L = strlen (title [k]);
For (j = 0; j <key [I]. Start; j ++)
Printf ("% C", title [k] [J]);
For (j = 0; j <L; j ++)
Printf ("% C", key [I]. Word [J]-32 );
For (j = Key [I]. Start + L; j <L; j ++)
Printf ("% C", title [k] [J]);
Printf ("\ n ");
}
}