Searching Quickly UVA 123

说说:感觉这题目是做得越来越繁琐了。这道题基本上把接下来课设要做的英语词典的框架给做出来了。好像本题的解法就是所谓的倒排索引。先给你一系列的句子,其实就是一系列的词啦。当然里面要把一些词去掉。然后把剩下的每个词都做成索引。最后按字典序把所有词所在的句子都输出就可以了。我的做法是定义了一个结构index包含关键词和一个指针,该指针指向一个链表,链表中的每个节点包含了该关键词所在的句子的位置,以及该关键词在句子中的位置。然后读入词,若不是要忽略的词就判断该词是否已经在关键词表中,在则添加到相应关键词的链表的末端,没有则将它添加到关键词列表中。最后将关键词排序,最后遍历关键词列表,根据索引输出关键词所在的句子即可。总的来说,挺繁琐的,不过只要一个一个模块做,一个一个模块调试,最终还是能解决的,哈哈~

题目:

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;
}

Searching Quickly UVA 123,布布扣,bubuko.com

时间: 2024-11-03 05:44:10

Searching Quickly UVA 123的相关文章

STL --- UVA 123 Searching Quickly

UVA - 123 Searching Quickly Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19296 Mean: 有一个字符串集合Ignore,还有一个文本集合TXT,在TXT中除了Ignore中的单词外其他的都是关键字,现在要你根据这些关键字来给TXT文本排序(根据关键字的字典). 注意:一行TXT文本中含多少个关键字就需要排多少次序,如果关键字的字典序相同则按照先后顺序来

UVA - 123 Searching Quickly

题目链接 这道题就是给定 一系列ignore词(全部是小写),以::结尾 然后  输入一系列文本,每行不包括ignore词的作为关键词,(与ignore词比较不区分大小写) ,然后排序输出.每一行中可能出现几个关键词,那就以出现顺序先后输出,如果有几行包括了同一个关键词,就以输入时顺序输出,其余的按照字典序排序输出.输出的时候时候除了关键词大写外,其余都要小写. 这道题做的时候有点长,不过幸好1A. 我的思路是先把文本全部转化为小写,然后取出关键词,同时保存它的初始位置在哪一行以及在这一行出现的

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

小白书练习题5.5.3 排序检索类、

UVA 340 Master-Mind Hints 题意:猜数字游戏,给n个数的序列给你.接下来一行是答案序列.剩下的都是猜测序列.对于每一个猜测序列,统计有多少个数字相同并且位置相同.有多少数字相同位置不同.每一个数字只能用一次. 思路:直接统计可以求出数字相同并且位置相同的哪一些数.在此过程中我加了一个标记数组.标记那些用过的数的位置为1,没用过为0:然后枚举猜测中哪些没用过的数字.去答案序列中找.当数字相等并且答案行中那个数也没用过时.计数加1: 1 #include<cstdio> 2

2015-05-29股票资料

代码 名称 股价 总市值 所属行业 6日涨幅% 6日换手% 总股本 601628 中国人寿 35.26 9966亿 保险 -3.85 2.77 283亿 601318 中国平安 85.44 7809亿 保险 -1.34 18.87 91.4亿 601601 中国太保 32.23 2921亿 保险 -3.85 10.88 90.6亿 601336 新华保险 58.81 1835亿 保险 -4.53 14.57 31.2亿 600660 福耀玻璃 16.43 412亿 玻璃陶瓷 1.55 23.92

UVa 12505 Searching in sqrt(n)

传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且用的原题. ------------------------------------------------------------------------------------------------------------------ time limit 5s In binary, the

uva 1597 Searching the Web

The word "search engine" may not be strange to you. Generally speaking, a search engine searches the web pages available in the Internet, extracts and organizes the information and responds to users' queries with the most relevant pages. World f

Searching the Web UVA - 1597

链接:https://vjudge.net/problem/UVA-1597#author=0 这题写了我一个晚上,然后debug了一个早上.. 最主要就是AND那一部分,一开始用了一个很奇怪的方法实现,就是利用set递增的性质,设置一个cur变量保存现在遍历到的文章下标的最大值,然后检查s1和s2能否取到,cur每次取当前s1和s2的文章下标最大值.中间实现的时候也出了点bug,没有在遍历到末尾的时候跳出循环.然而这不是重点..重点在于cur不一定取到,也就是可以跳过cur取一个更大的值, 这

uva 11374

Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Commercial-Xpres