UVA 123 Searching Quickly

题意:给出一系列要忽略的单词,这些单词以外的单词都看作关键字。然后给出一些标题,找出标题中所有的关键字,然后按这些关键字的字典序给标题排序。

注意两点:相同关键字出现在不同标题中,出现在输入较前位置的标题排在前面(从样例数据可以看出,而且multimap也正是这么做的);同一个关键字在一个标题中出现多次,关键字位于较前位置的排在前面(从左向右扫描的话就没有问题)。

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 ecient O(n log n) [average case] comparison based sort.
KWIC-indexing is an indexing method that permits ecient \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.ACM Contest Problems Archive University of Valladolid (SPAIN)
The keyword should appear in all upper-case letters. All other words in a title should be in lowercase letters. Titles in the KWIC-index with the same keyword should 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 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 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)strlen
int 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;
}



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值