ZJU 1109 Language of FatMouse

本文介绍了一种使用快速排序(qsort)和二分查找(BinarySearch)来处理大规模数据的方法,通过具体代码实例展示了如何读取并组织字典数据,然后对数据进行排序和搜索的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析:不要被规模吓了,用快排和二分查找可以解决。

 

ContractedBlock.gifExpandedBlockStart.gifCode
#include <iostream>
using namespace std;

int size=0;
struct word
ExpandedBlockStart.gifContractedBlock.gif
char English[11],FatMouse[11]; }dictionary[100006];
void InputWord(char* str)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int len = strlen(str),i;
    
for(i=0;i<len;++i)
        
if(str[i] == ' ')
            
break;

    strncpy(dictionary[size].English,str,i);
    dictionary[size].English[i] 
= '\0';
    
    len 
= len-i-1;
    strncpy(dictionary[size].FatMouse,str
+i+1,len);
    dictionary[size].FatMouse[len] 
= '\0';
}

char* BinarySearch(int begin,int end,char* str)
ExpandedBlockStart.gifContractedBlock.gif
{
    
if(begin>end)
        
return 0;
    
int mid = (begin + end)/2;
    
int flag;
    
if((flag=strcmp(dictionary[mid].FatMouse,str)) == 0)
        
return dictionary[mid].English;
    
if(flag<0)
        
return BinarySearch(mid+1,end,str);
    
else
        
return BinarySearch(begin,mid-1,str);
}

int cmp(const void* l,const void* r)
ExpandedBlockStart.gifContractedBlock.gif
return strcmp(((word*)l)->FatMouse,((word*)r)->FatMouse); }

int main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
char str[50];
    
while(gets(str),strlen(str)!=0)
        InputWord(str),
++size;
    qsort(dictionary,size,
sizeof(word),cmp);
    
while(cin>>str)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
char* w = BinarySearch(0,size-1,str);
        cout
<<(w?w:"eh")<<endl;
    }

    
return 0;
}

转载于:https://www.cnblogs.com/jaskist/archive/2009/05/02/1447760.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值