online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include<bits/stdc++.h> using namespace std; class node { public: char data; bool terminal; map<char, node*>children; node(char data) { this->data = data; this->terminal = false; } }; class tries { public : node *root; tries() { this->root = new node('\0'); } void addword(string str) { node *temp = root; for (int i = 0; i < str.length(); i++) { char ch = str[i]; if (temp->children.count(ch)) { temp = temp->children[ch]; } else { node *newnode = new node(ch); temp->children[ch] = newnode; temp = newnode; } } temp->terminal = true; } }; void print(node *root, string s) { if (root == NULL) { return ; } if (root->data != '\0') { s += root->data; } if (root->terminal) { // s += root->data; cout << s << endl; for (auto p : root->children) { print(p.second, s); } return; } if (root->children.empty()) { // s += root->data; cout << s << endl; return; } for (auto p : root->children) { print(p.second, s); } // if (root->terminal){ // cout << s << endl; // } } bool searchprefix(node *root, string str) { node *temp = root; for (int i = 0; i < str.length(); i++) { char ch = str[i]; if (temp->children.count(ch)) { temp = temp->children[ch]; } else { return false; } } for(auto p : temp->children){ print(p.second, str); } if (temp->terminal){ cout << str << endl; } // print(temp, str); return true; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif tries *t = new tries(); int n; cin >> n; string arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; t->addword(arr[i]); } int q; cin >> q; for (int i = 0; i < q; i++) { string temp; cin >> temp; bool p = searchprefix(t->root, temp); if (p == false) { cout << "No suggestions" << endl; t->addword(temp); } } return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue