Someone experienced who understands string manipulation well, who can help with this error

Asked

Viewed 75 times

-1

Good morning, I am presenting a problem that I have no idea the reason, I have tried to ask other people, none discovered the real reason for the error and how to repair it. Good my teacher, asked to implement a system with which opens a txt file that contains words and those words should be stored in a trie tree. The implementation of this tree with a test is available at: https://www.geeksforgeeks.org/trie-insert-and-search/

Thus, the implementation of this tree is very close to what needs to be done, since through a string array the words are inserted into the tree. Below the "main" function of the implementation I provided from the link, the output of this program is all correct, indicating that there is the word "These" and there is the word "the"

[Imagem da implementação da internet, tudo funciona corretamente[1]

So basically I adapted the code by reading the txt file and it didn’t work, I believe the problem may be related to the Insert method. I did a test too, printing all the words of the file and fortunately it worked, IE the problem is only in inserting the words

inserir a descrição da imagem aqui

It doesn’t make sense for every word to be right on its way out. And at the time of telling if this word exists or not in the tree does not work, I used the same implementation that was working correctly. I’d really appreciate it if someone knew what I did wrong

  • 6

    https://pt.meta.stackoverflow.com/a/5485/112052

  • 1

    Please [Edit] and post the code as text. Then select and click the button {} editor to format.

1 answer

0

First of all I always recommend to post the code itself and not the image, it makes it easier to try to simulate and help in the solution. I tried to play your code and realized that the error is in the regex regular expression. In fact the expression you defined didn’t even get to run in my test (I’m using visual studio 2017, I don’t know if it’s a problem for the environment). Anyway I managed to execute in the expected way using the code below, where I needed to change the regex regular expression.

int main()
{
    // Input keys (use only 'a' through 'z' 
    // and lower case) 
    string subject = "the a there answer any by bye their";

    struct TrieNode *root = getNode();

    // Construct trie 
    regex re = regex("\\w+");
    sregex_iterator next(subject.begin(), subject.end(), re);
    sregex_iterator end;
    int i = 0;

    while (next != end) {

        smatch match = *next;
        string palavra = match.str();
        replace(palavra.begin(), palavra.end(), '[', '\0');
        replace(palavra.begin(), palavra.end(), ']', '\0');

        cout << palavra << " " << i << endl;
        insert(root, palavra);
        next++;
        i++;
    }

    // Search for different keys 
    search(root, "the") ? cout << "Yes\n" :
        cout << "No\n";
    search(root, "these") ? cout << "Yes\n" :
        cout << "No\n";
    return 0;
}

Follow picture of output:

Output

Browser other questions tagged

You are not signed in. Login or sign up in order to post.