uva 123 - Searching Quickly

来源:互联网 发布:2017国二c语言题库 编辑:程序博客网 时间:2024/05/21 10:18
 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 efficienttex2html_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 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 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

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

题目不难就是烦了点,给以一组可忽略的单词,再给你一组句子,除了可忽略的单词剩余的都是关键字,每次输出一个关键字按字典序输出。

本来想多关键字快排,不太熟老错,然后全部做完之后提交runtime error 无数次一直以为是数组开小了,开了一次又一次还是wrong。

今天百度别人的发现读入句子的时候用getline,而我用gets貌似之前这个错过,于是改成fgets,过了.......坑啊runtime error 三十多次大哭

#include <stdio.h>
#include <string.h>
char ignore[51][20],title[210][1000],temp[30];
struct s
{char word[30];
 int  postion,start;
}key[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");
 } 
}

原创粉丝点击