UVA 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


一道挺复杂的搜索排序题,给一些忽略的词,找出title中非忽略词(关键词),将title按关键词排序并将title输出(关键词大写)。我是先将整个input输入,然后遍历每个句子,将每个关键词存入另一个关键词数组。再创一个关键词数组副本,将原本排序。重点是将排序后的关键词数组和它以前所在的title联系起来。所以我设了一个记录位置的数组,数组的下标为该关键词在关键词数组中的位置,而数组的值为该关键词所在的title的位置。这样遍历排序后的关键词数组,对每个排序后关键词在原本中找到它的位置,从而找到他的title的位置。在title中找到该词,将title输出(该词大写,其它小写输出)。对每个找过的词都在title中标记为大写,在原本中也标记为大写(防止重复查找)。由于查找忽略大小写,输入时统一转化为小写。

题目虽然复杂,但思路很清楚,一遍AC了。☺

AC的代码如下:

#include 
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
int cmp(const void*a,const void*b)
{
    return strcmp((char *)a,(char *)b);
}
char title[200][200],ignore[50][11],temp[20],c,keyword[5000][20],keyword2[5000][20],temp2[200];
int main()
{
    int i=0,j=0,p,q,m=0,k=0,r=0,flag1[5000],a=0,count1=0,g,t=0,h;
    while(c=getchar())
    {
        if(isalpha(c))
            ignore[i][j++]=c;
        else if(c==':')
            break;
        else if(c=='\n')
        {
            ignore[i][j]='\0';
            i++;
            j=0;
        }
    }
    p=i;
    getchar();
    getchar();
    i=0;
    j=0;
    while((c=getchar())!=EOF)
    {
        if(c=='\n')
        {
            title[i][j]='\0';
            i++;
            j=0;
        }
        else
        {
            c=tolower(c);
            title[i][j++]=c;
        }
    }
    q=i;
    int ok=1,ok2=0;
    for(i=0; i<=q-1; i++)
        for(j=0; j<=strlen(title[i]); j++)
        {
            if(title[i][j]!=' '&&title[i][j]!='\0')
                temp[m++]=title[i][j];
            else
            {
                temp[m]='\0';
                for(k=0; k<=p-1; k++)
                    if(strcmp(temp,ignore[k])==0)
                    {
                        ok=0;

                        break;

                    }
                if(ok==1)
                {
                    strcpy(keyword[r++],temp);
                    flag1[a++]=i;
                    memset(temp,'\0',sizeof(temp));
                }
                m=0;
                ok=1;
            }
        }
    for(i=0; i<=r-1; i++)
        strcpy(keyword2[i],keyword[i]);
    qsort(keyword,r,20,cmp);
    for(i=0; i<=r-1; i++)
    {
        for(j=0; j<=r-1; j++)
        {
            if(strcmp(keyword[i],keyword2[j])==0)
            {
                strcpy(temp2,title[flag1[j]]);
                for(int j1=0; j1<=strlen(keyword[i])-1; j1++)
                {
                    int flag=0;
                    for(int i1=0; i1<=strlen(temp2)-1; i1++)
                    {
                        if(keyword[i][j1]==temp2[i1])
                        {
                            if(flag==0)
                                g=i1;
                            count1++;
                            j1++;
                            t++;
                            h=i1;
                            flag=1;
                            if(count1>=strlen(keyword[i])&&(temp2[i1+1]==' '||temp2[i1+1]=='\0'))
                            {
                                int i2;
                                for(i2=0; i2<=g-1; i2++)
                                    printf("%c",tolower(temp2[i2]));
                                for(i2=g; i2<=h; i2++)
                                    printf("%c",toupper(temp2[i2]));
                                for(i2=h+1; i2<=strlen(temp2)-1; i2++)
                                    printf("%c",tolower(temp2[i2]));
                                printf("\n");
                                for(i2=g; i2<=h; i2++)
                                    title[flag1[j]][i2]=toupper(title[flag1[j]][i2]);
                                ok2=1;
                                for(i2=0; i2<=strlen(keyword2[j])-1; i2++)
                                    keyword2[j][i2]=toupper(keyword2[j][i2]);
                                break;

                            }
                        }
                        else
                        {
                            count1=0;
                            j1=j1-t;
                            t=0;
                            ok2=0;
                            flag=0;
                        }

                    }
                    if(ok2==1)
                        break;
                }
            }
            if(ok2==1)
                break;
        }
        memset(temp2,'\0',sizeof(temp2));
        ok2=0;
        t=0;
    }
    return 0;
}

      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值