【数据结构与算法】【小题】3个查找算法

【问题描述】
从标准输入中读入一个英文单词,在一个给定的英文常用单词字典中查找该单词,返回查找结果(查找到返回1,否则返回0)和查找过程中单词的比较次数。查找前,先将字典文件中所有单词读入至一个单词表(数组)中,然后按要求进行查找。字典中单词总数不超过3500,单词中的字符都是英文小写字母,并已按字典序排好序。字典中的单词和待查找单词的字符个数不超过20。

【输入形式】
读入待查找单词。只包含英文小写字母。

【输出形式】
将查找结果和单词比较次数输出到标准输出上,两整数之间以一个空格分隔。
【样例输入与输出】

运行结果测试:
在这里插入图片描述

代码:

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;

typedef struct {
    char index;
    int start;
    int length;
}item;

// 二分查找
vector<int> binarySearch(vector<string>& words, int left, int right, string target) {
    int mid, count = 0;
    while (left <= right) {
        count++;
        mid = left + (right - left) / 2;
        if (target == words[mid]) return vector<int>{1, count};
        if (target < words[mid]) right = mid - 1;
        else left = mid + 1;
    }
    return vector<int>{0, count};
}

// 索引表查找
vector<int> indexSearch(vector<string>& words, string target) {
    vector<item> table;
    int i, j = 1, n = words.size();
    item temp = { words[0][0],0, };
    table.push_back(temp);
    for (i = 1; i < n; ++i) {
        if (words[i][0] != words[i - 1][0]) {
            temp.index = words[i][0];
            temp.start = i;
            table[j - 1].length = i - table[j - 1].start;
            table.push_back(temp);
            j++;
        }
    }
    table[j - 1].length = n - table[j - 1].start;
    /*for (int i = 0; i < j; i++) { //输出索引表
        cout << i << "\t" << "index:" << table[i].index << "\t"
            << "start:" << table[i].start << "\t"
            << "length:" << table[i].length << endl;
    }*/
    for (i = 0; i < j; ++i) { // 先在索引表中找
        if (table[i].index == target[0]) break;
        if (table[i].index > target[0]) return vector<int>{0, i + 1};
    }
    int left = table[i].start;
    int right = left + table[i].length - 1;
    vector<int> result = binarySearch(words, left, right, target); // 再二分查找
    result[1] += i + 1;
    return result;
}

// 顺序查找
vector<int> seqSearch(vector<string>& words, string target) {
    int count = 0;
    for (auto word : words) {
        count++;
        if (word == target) return vector<int>{1, count};
        if (word > target) return vector<int>{0, count};
    }
    return vector<int>{0, count};
}

int main() {
    ifstream ifs;
    string buf, target;
    vector<string> words;
    vector<int> result1, result2, result3;

    ifs.open("dictionary3000.txt", ios::in);
    if (!ifs.is_open()) { cout << " file open fail!" << endl; return 0; }
    while (getline(ifs, buf)) words.push_back(buf);

    while (cin >> target) {
        result1 = binarySearch(words, 0, words.size() - 1, target);
        result2 = indexSearch(words, target);
        result3 = seqSearch(words, target);
        cout << "二分查找:\t" << result1[0] << " " << result1[1] << endl;
        cout << "索引表查找:\t" << result2[0] << " " << result2[1] << endl;
        cout << "顺序查找:\t" << result3[0] << " " << result3[1] << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值