UVA - 123 - Searching Quickly

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int count[3010],gap[200]; 
int sen=0;     
int cmp_string(const void *_a,const void *_b)
{
    char *a=(char *)_a;
    char *b=(char *)_b;
    return strcmp(a,b);
} 
int cmp_char(const void *_a,const void *_b)
{
    char *a=(char *)_a;
    char *b=(char *)_b;
    return *a-*b;
}  
int input(char source[][20],int &sen)
{
    int i=0,j=0,k=1;
    char c;
    while((c=getchar())!=EOF)
    {
        if(isalpha(c))
        {
            source[i][j++]=tolower(c);
        }
        else if(c==' '||c=='\n')
        {
            if(c=='\n')gap[k++]=i;
            i++;
            j=0;
        }
    }
    gap[k]=i-1;
    sen=k;
    return i;
} 
int Extract(const char a[][20],const char b[][20],char c[][20],const int asum,const int bsum)
{
    int i,j,k=0;
    for(i=0;i<bsum;i++)
    {
        for(j=0;j<asum;j++)
        {
            if(strcmp(a[j],b[i])==0)
            {
                count[j]=1;
            }
        }
    }
    for(i=0;i<asum;i++)
    {
        if(!count[i])strcpy(c[k++],a[i]);
    }
    return k;
} 
void OutPut(const char a[][20],int pos,int key)
{
    int i,j,k; 
    for(i=gap[pos];i<=gap[pos+1];i++)
    {
        if(i!=key)
        {
            for(j=0;j<strlen(a[i]);j++)
            {
                printf("%c",a[i][j]);
            }
            if(i!=gap[pos+1])printf(" ");
            else printf("\n");
        }
        else
        {
            for(j=0;j<strlen(a[key]);j++)
            {
                printf("%c",a[key][j]-32);
            }
            if(i!=gap[pos+1])printf(" ");
            else printf("\n");
        } 
    }
}           
                        
void SearchOut(const char a[][20],const int asum,char b[][20],int bsum)
{
    int i,j,k;
    for(k=0;k<bsum;k++)
    {
        for(i=0;i<sen-1;i++)
        {
            for(j=gap[i];j<=gap[i+1];j++)
            {
                if(strcmp(a[j],b[k])==0)OutPut(a,i,j);
            }
        }
    }    
}                                 
int main()
{
    freopen("123in.txt","r",stdin);
    freopen("123out.txt","w",stdout);
    char source[3010][20],neg[60][20],save[3010][20];
    int i=0,j=0,k=0,sum,tag;
    for(k=0;k<3010;k++){if(k<200)gap[k]=0;count[k]=0;}   
    do
    {
        gets(neg[i++]);
    }while(neg[i-1][0]!=':');
    i--; 
    //for(j=0;j<i;j++)puts(neg[j]);    
    sum=input(source,sen);
    //for(j=0;j<sum;j++)puts(source[j]);
    
    tag=Extract(source,neg,save,sum,i);
    qsort(save,tag,sizeof(save[0]),cmp_string);
    //for(j=0;j<tag;j++)puts(save[j]);
    SearchOut(source,sum,save,tag);
    return 0;
}                
        
    


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值