首页 > 代码库 > Searching Quickly UVA 123
Searching Quickly UVA 123
说说:感觉这题目是做得越来越繁琐了。这道题基本上把接下来课设要做的英语词典的框架给做出来了。好像本题的解法就是所谓的倒排索引。先给你一系列的句子,其实就是一系列的词啦。当然里面要把一些词去掉。然后把剩下的每个词都做成索引。最后按字典序把所有词所在的句子都输出就可以了。我的做法是定义了一个结构index包含关键词和一个指针,该指针指向一个链表,链表中的每个节点包含了该关键词所在的句子的位置,以及该关键词在句子中的位置。然后读入词,若不是要忽略的词就判断该词是否已经在关键词表中,在则添加到相应关键词的链表的末端,没有则将它添加到关键词列表中。最后将关键词排序,最后遍历关键词列表,根据索引输出关键词所在的句子即可。总的来说,挺繁琐的,不过只要一个一个模块做,一个一个模块调试,最终还是能解决的,哈哈~
题目:
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 [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
源代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct pos{//索引,存储关键词所在的句子位置以及它在句子中的位置 int row;//title位置 int cow;//关键词在title中的位置 struct pos*next; }POS; typedef struct index{//存储关键词 char word[55]; POS* head;//指向该关键词索引链表的头指针 }INDEX; INDEX list[3000+5]; char ignored[55][15];//需要忽略的单词 char title[220][20][50];//存储句子 int title_len[220];//存储每个 int ignore_num=0;//需要被忽略的单词的个数 int title_num=0;//title个数 int key_num=0;//关键词个数 void get_ignore();//获取需要被忽略的词 void get_title();//获取title int Ignore(char*);//判断一个单词是否需要被忽略 int Exist(char*);//判断该关键词是否已在关键词表中 void convert(char*); void Add_key(char*,int,int);//添加新的关键词 void Add_element(int,int,int);//向已有关键词添加新的索引 void Sort(); int main(){ int i,j,k; int Row,Num,Cow; POS* p; // freopen("data","r",stdin); get_ignore(); get_title(); Sort();//关键词排序 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':' ');//输出大写的关键词 else printf("%s%c",title[Row][j],j==title_len[Row]-1?'\n':' '); p=p->next; } } return 0; } void get_ignore(){//获取要忽略的词 ignore_num=0; while(scanf("%s",ignored[ignore_num++])&&ignored[ignore_num-1][0]!=':'); ignore_num--; } void get_title(){//获取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]);//将title中的词都转化为小写 if(!Ignore(title[title_num][cow_cnt])){//若不是要忽略的词 if((pos=Exist(title[title_num][cow_cnt]))==-1)//不是已有的关键词 Add_key(title[title_num][cow_cnt],title_num,cow_cnt); else//已存在在现有的关键词,则加索引 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){//是否是要忽略的词 int i; for(i=0;i<ignore_num;i++) if(strcmp(word,ignored[i])==0) return 1; return 0; } int Exist(char *word){//是否已在关键词表中 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){//转化为小写字母形式 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)//将新索引添加在索引链表的末端,题目有要求 p=p->next; new->next=p->next; p->next=new; } void Add_key(char *Word,int Row,int Cow){//创建新关键词 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(){//将关键词表排序 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; }