UVA123 Searching Quickly

这题真的是一把辛酸泪啊,好复杂,做了两天。个人算法比较弱啊,看代码量就知道了。先提取关键字,然后储存题目下标。把关键字排序,储存题目下标的数组排序,最后输出来。

脑子快做炸了,但是最后还是AC了。

题目:

Searching Quickly

 

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

 

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

 

解答:

 

  1 #include<ctype.h>
  2 #include<stdlib.h>
  3 #include<stdio.h>
  4 #include<string.h>
  5 struct key
  6 {
  7     char word[50];
  8     int cnt[10];
  9     int len;
 10 };
 11 int cmp_key(const void *a,const void *b)
 12 {
 13     return strcmp(((key*)a)->word,((key*)b)->word);
 14 }
 15 int cmp_int(const void *a,const void *b)
 16 {
 17     return (*(int*)a)-(*(int*)b);
 18 }
 19 key ky[20000];
 20 char ig[55][20],sav[210][10010],tmp[100];
 21 int main()
 22 {
 23     int i=0,j,k,in,sn;
 24     for(;;)
 25     {
 26         scanf("%s",ig[i]);
 27         if(strcmp(ig[i],"::")==0)
 28             break;
 29         i++;
 30     }
 31     in=i;
 32     i=0;
 33     getchar();
 34     while(fgets(sav[i],sizeof(sav[0]),stdin)!=NULL)
 35     {
 36         for(j=0;j<strlen(sav[i]);j++)
 37             sav[i][j]=tolower(sav[i][j]);
 38         i++;
 39     }
 40     sn=i;
 41     int tp=0,m=0,kp=0,ii;
 42     for(i=0;i<sn;i++)
 43     {
 44         for(j=0;j<strlen(sav[i]);j++)
 45         {
 46             if(sav[i][j]==' '||sav[i][j]=='\n')
 47             {
 48                 tmp[tp]='\0';
 49                 int flag=0;
 50                 for(k=0;k<in;k++)
 51                 {
 52                     if(strcmp(tmp,ig[k])==0)
 53                     {
 54                         flag=1;
 55                         break;
 56                     }
 57                 }
 58                 if(flag==0)
 59                 {
 60                     int f=0;
 61                     for(k=0;k<kp;k++)
 62                     {
 63                         if(strcmp(tmp,ky[k].word)==0)
 64                         {
 65                             int ii,fg=0;
 66                             for(ii=0;ii<ky[k].len;ii++)
 67                             {
 68                                 if(ky[k].cnt[ii]==i)
 69                                 {
 70                                     fg=1;
 71                                     f=1;
 72                                     break;
 73                                 }
 74                             }
 75                             if(fg==0)
 76                             {
 77                                 ky[k].cnt[ky[k].len]=i;
 78                                 ky[k].len++;
 79                                 f=1;
 80                                 break;
 81                             }
 82 
 83                         }
 84                     }
 85                     if(f==0)
 86                     {
 87                         strcpy(ky[kp].word,tmp);
 88                         ky[kp].len=0;
 89                         ky[kp].cnt[ky[kp].len]=i;
 90                         ky[kp].len++;
 91                         kp++;
 92                     }
 93                 }
 94                 tp=0;
 95             }
 96             else
 97             {
 98                 tmp[tp]=sav[i][j];
 99                 tp++;
100             }
101         }
102     }
103     qsort(ky,kp,sizeof(key),cmp_key);
104     for(i=0;i<kp;i++)
105         qsort(ky[i].cnt,ky[i].len,sizeof(int),cmp_int);
106     for(i=0;i<kp;i++)
107     {
108         for(j=0;j<ky[i].len;j++)
109         {
110             tp=0;
111             for(ii=0;ii<strlen(sav[ky[i].cnt[j]]);ii++)
112             {
113                 if(sav[ky[i].cnt[j]][ii]==' '||sav[ky[i].cnt[j]][ii]=='\n')
114                 {
115                     tmp[tp]='\0';
116                     if(strcmp(tmp,ky[i].word)==0)
117                     {
118                         int jj;
119                         for(jj=0;jj<strlen(sav[ky[i].cnt[j]]);jj++)
120                         {
121                             if(jj==(ii-strlen(tmp)))
122                             {
123                                 while(sav[ky[i].cnt[j]][jj]!=' '&&sav[ky[i].cnt[j]][jj]!='\n')
124                                 {
125                                     printf("%c",toupper(sav[ky[i].cnt[j]][jj]));
126                                     jj++;
127                                 }
128                             }
129                             printf("%c",sav[ky[i].cnt[j]][jj]);
130                         }
131                     }
132                     tp=0;
133                 }
134                 else
135                 {
136                     tmp[tp]=sav[ky[i].cnt[j]][ii];
137                     tp++;
138                 }
139             }
140         }
141     }
142     return 0;
143 }

 

 

 

posted @ 2013-02-19 16:19  上白泽慧音  阅读(337)  评论(0编辑  收藏  举报