Algorithm na Binciken Rubutu (Text Search) a C++- Bayani, Misali da Lamba

Algorithm na Binciken Rubutu hanya ce ta asali a sarrafa rubutu da dawo da bayanai. Wannan algorithm yana ba mu damar ganowa da gano abubuwan da suka faru na ƙaramin igiya(ko tsari) a cikin babban yanki na rubutu.

Yadda Ake Aiki

  1. Fara da babban yanki na rubutu(ko daftarin aiki) da ƙaramin igiya don nema.
  2. Yi maimaita ta kowane hali na rubutun a jere.
  3. Kwatanta ƙaramin kirtani tare da wani yanki na rubutun wanda ke da tsayi iri ɗaya da igiyar ƙasa. Idan an sami wasa, yi rikodin matsayi na yanzu.
  4. Matsar zuwa matsayi na gaba kuma ci gaba da kwatanta.

Misali

Yi la'akari da rubutun: "Bari mu nemo maƙasudin ƙira a cikin wannan rubutun don ganin yadda algorithm ke aiki."

Da kuma gunkin da ake nema: "substring"

  1. Fara a matsayi 0. Kwatanta Let' da "substring". Babu wasa.
  2. Matsar zuwa matsayi 1. Kwatanta et's da "ƙarshen igiya". Babu wasa.
  3. Matsar zuwa matsayi 2. Kwatanta t's " tare da "ƙarshen igiya". Babu wasa.
  4. Matsar zuwa matsayi 3. Kwatanta 's s da "ƙarshen igiya". Babu wasa.
  5. Matsar zuwa matsayi na 4. Kwatanta se da "ƙarshen igiya". Babu wasa.
  6. Matsar zuwa matsayi na 5. Kwatanta sea da "ƙarshen igiya". Babu wasa.
  7. Matsar zuwa matsayi na 6. Kwatanta earc da "ƙarshen igiya". Babu wasa.
  8. Matsar zuwa matsayi na 7. Kwatanta arch da "ƙarshen igiya". Babu wasa.
  9. Matsar zuwa matsayi 8. Kwatanta rch "tare da "ƙarshen igiya" Babu wasa.
  10. Matsar zuwa matsayi na 9. Kwatanta ch w da "ƙarshen igiya". Babu wasa.
  11. Matsar zuwa matsayi na 10. Kwatanta h wi da "ƙarshen igiya". Babu wasa.
  12. Matsar zuwa matsayi na 11. Kwatanta " wit da "ƙarshen igiya". Babu wasa.
  13. Matsar zuwa matsayi na 12. Kwatanta with da "ƙarshen igiya". Babu wasa.
  14. Matsar zuwa matsayi na 13. Kwatanta ithi da "ƙarshen igiya". Babu wasa.
  15. Matsar zuwa matsayi na 14. Kwatanta thin da "ƙarshen igiya". Babu wasa.
  16. Matsar zuwa matsayi na 15. Kwatanta hinn da "ƙarshen igiya". Babu wasa.
  17. Matsar zuwa matsayi na 16. Kwatanta in t da "ƙarshen igiya". Babu wasa.
  18. Matsar zuwa matsayi na 17. Kwatanta n th da "ƙarshen igiya". Babu wasa.
  19. Matsa zuwa matsayi na 18. Kwatanta " thi da "ƙarshen igiya". Babu wasa.
  20. Matsar zuwa matsayi na 19. Kwatanta this da "ƙarshen igiya". Babu wasa.
  21. Matsa zuwa matsayi na 20. Kwatanta his " da "ƙarshen igiya". Babu wasa.
  22. Matsar zuwa matsayi na 21. Kwatanta is t da "substring". Babu wasa.
  23. Matsar zuwa matsayi na 22. Kwatanta s te  da "ƙarshen igiya". Babu wasa.
  24. Matsar zuwa matsayi na 23. Kwatanta ubst  da "ƙarshen igiya". Babu wasa.
  25. Matsar zuwa matsayi na 24. Kwatanta bstr da "ƙarshen igiya". Babu wasa.
  26. Matsar zuwa matsayi na 25. Kwatanta stre da "ƙarshen igiya". Babu wasa.
  27. Matsar zuwa matsayi na 26. Kwatanta trin  da "ƙarshen igiya". Babu wasa.
  28. Matsar zuwa matsayi na 27. Kwatanta ring  da "ƙarshen igiya". An samo wasa, matsayi na 27.

Ana samun "ƙarshen igiya" a matsayi na 27 a cikin rubutun.

Misali Code a C++

#include <iostream>  
#include <string>  
#include <vector>  
  
std::vector<int> textSearch(const std::string& text, const std::string& query) {  
    std::vector<int> positions;  
  
    for(int i = 0; i <= text.length()- query.length(); ++i) {  
        int j = 0;  
        while(j < query.length() && text[i + j] == query[j]) {  
            ++j;  
        }  
        if(j == query.length()) {  
            positions.push_back(i);  
        }  
    }  
  
    return positions;  
}  
  
int main() {  
    std::string text = "Let's search for a substring within this text to see how the algorithm works.";  
    std::string query = "substring";  
  
    std::vector<int> result = textSearch(text, query);  
  
    std::cout << "Substring found at positions: ";  
    for(int pos: result) {  
        std::cout << pos << ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}  

A cikin wannan misali, textSearch ana amfani da aikin don nemo matsayin "substring" a cikin rubutun. Sakamakon zai zama vector mai kunshe da wuraren farawa na matches.