// Busca palabras en el diccionario con std::lower_bound y cuenta las comparaciones.
// Uso: word_search archivo_de_palabras palabra...
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main(int argc, char** argv) {
    if (argc < 3) {
        std::cerr << "uso: word_search archivo_de_palabras palabra...\n";
        return 2;
    }
    std::ifstream file(argv[1]);
    std::vector<std::string> words;
    std::string line;
    while (std::getline(file, line)) {
        words.push_back(line);
    }
    if (words.empty()) {
        std::cerr << "no se pudo leer " << argv[1] << "\n";
        return 1;
    }
    std::printf("%zu palabras; ordenado por bytes: %s\n", words.size(),
                std::is_sorted(words.begin(), words.end()) ? "sí" : "no");
    for (int i = 2; i < argc; i++) {
        std::string wanted = argv[i];
        long long comparisons = 0;
        auto found = std::lower_bound(words.begin(), words.end(), wanted,
                                      [&comparisons](const std::string& word, const std::string& x) {
                                          comparisons++;
                                          return word < x;
                                      });
        bool present = found != words.end() && *found == wanted;
        std::printf("%-10s %2lld comparaciones, %s", wanted.c_str(), comparisons, present ? "está" : "no está");
        if (!present && found != words.end()) {
            std::printf("; la siguiente es %s", found->c_str());
        }
        std::printf("\n");
    }
    return 0;
}
