POJ 2503 二分 还有一些基本字符串的处理

本文介绍了一种使用C++实现的字典查找算法。该算法通过二分法搜索预先定义好的字典项来确定输入字符串的位置。文章提供的代码实现了从文件中读取并解析字符串数据,然后对这些字符串进行排序,并利用二分查找方法快速定位特定的字符串。

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


二分嘛,还是 wata 版本的。


只要二分查找字符应该出现的位置就好了。就是查字典

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <assert.h>
#include <string>
using namespace std;

struct P
{
    char e[30];
    char f[30];
    bool operator < (const P &o) const 
    {
        if(strcmp(f,o.f)>=0)
            return 0;
        return 1;
    }
}dic[200000];
void mySubstr(char* f,char* t,int s,int e)
{
    for(int i=0;i<=e-s;i++)
        t[i]=f[s+i];
    t[e+1]='\0';
}
int cnt;

int main()
{
    freopen("acm.in","r",stdin);
    char t[50];
    while(gets(t))
    {
        cnt=0;
        do
        {
            int pos=0;
            while(t[pos]!=' ')
                pos++;
            mySubstr(t,dic[cnt].e,0,pos-1);
            mySubstr(t,dic[cnt].f,pos+1,strlen(t)-1);
            cnt++;
        }while(gets(t)&&t[0]!='\0');
        sort(dic,dic+cnt);
        while(gets(t))
        {
            int lb=0,ub=cnt;
            while(ub-lb>1)
            {
                int m=(lb+ub)>>1;
                if(strcmp(dic[m].f,t)>0)
                    ub=m;
                else
                    lb=m;
            }
            if(strcmp(dic[lb].f,t)==0)
                printf("%s\n",dic[lb].e);
            else
                puts("eh");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值