Ultraviolet A 123 Searching Quickly

Source: Internet
Author: User

A series of words to be ignored. Words other than these words are considered as keywords. Then, we give some titles, find all the keywords in the titles, and sort the titles in the Lexicographic Order of these keywords.

Note two points: the same keyword appears in different titles, and the title appears in the front of the input (as shown in the sample data, and multimap does the same ); the same keyword appears multiple times in a title, and the keyword is in the front of the previous position (if you scan left to right, there is no problem ).

ACM Contest Problems Archive University of Valladolid (SPAIN)
123 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 e cient O (n log n) [average case] comparison based sort.
KWIC-indexing is an indexing method that permits e cient \ 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 Man
The Ascent of Man
The Old Man and The Sea
A 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
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
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
Title, each instance is a potential keyword. ACM Contest Problems Archive University of Valladolid (SPAIN)
The keyword shoshould appear in all upper-case letters. all other words in a title shoshould be in lowercase letters. titles in the KWIC-index with the same keyword shoshould appear in the same order as they
Appeared in the input le. 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 justi ed or aligned by keyword, all titles may be listed
Left-justi ed.
Sample Input
Is
The
Of
And
As
A
But
::
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
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>#include<cctype>#include<algorithm>char ignore[55][15],title[205][1005],keywords[1001][105];int num[1001];#define strlen (int)strlenint find1(int n,char *s){    for(int i=0;i<n;i++)        if(strcmp(s,ignore[i])==0)  return 0;    return 1;}int cmp(const void *s1,const void *s2){    return strcmp((char*)s1,(char*)s2);}int main(){    freopen("in.txt","r",stdin);char ch[15];int n=0;while(1)    {        gets(ch);        if(strcmp(ch,"::")==0)break;        memcpy(ignore[n++],ch,sizeof(ch));    }    int f=-1,n2=0;    while(gets(title[++f])!=NULL)    {        int c=0;        for(int i=0;i<=strlen(title[f]);i++){                if(title[f][i]>='A'&&title[f][i]<='Z')title[f][i]+='a'-'A';                if(isalpha(title[f][i]))ch[c++]=title[f][i];                else                {                    ch[c]='\0';                    c=0;                    if(find1(n,ch))memcpy(keywords[n2++],ch,sizeof(ch));                }        }    }    qsort(keywords,n2,sizeof(keywords[0]),cmp);    memset(num,0,sizeof(num));    for(int i=0;i<n2;i++)    {        num[i]=1;int j=1;        while(i+j<n2&&strcmp(keywords[i+j],keywords[i])==0)        {            num[i]++;            j++;        }        i+=j-1;    }    for(int i=0;i<n2;i++)    {        int t1=1;        if(num[i])for(int j=0;j<=f;j++)        {            int t=0;            for(int k=0;k<=strlen(title[j]);k++)            {                if(isalpha(title[j][k]))                    ch[t++]=title[j][k];                else                {                    ch[t]='\0';                    if(strcmp(ch,keywords[i])==0)                    {                        for(int I=0;I<k-t;I++)putchar(title[j][I]);                        for(int I=0;I<strlen(ch);I++)putchar(ch[I]+'A'-'a');                        puts(&title[j][k]);                        t1++;                    }                    t=0;                }                if(t1>num[i])break;            }        }    }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.