Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <map>
- #include <codecvt>
- #include <locale>
- using namespace std;
- const int ALPHABET_SIZE = 36;
- map<wchar_t, int> alphabet;
- struct node {
- node * children_of_node[ALPHABET_SIZE];
- bool is_end_of_word;
- node() {
- is_end_of_word = false;
- for(int i = 0; i < ALPHABET_SIZE; i++) {
- children_of_node[i] = NULL;
- }
- }
- };
- void add_word(node * trie, wstring word) {
- node * tmp = trie;
- for(int i = 0; i <(int) word.length(); i++) {
- int c = alphabet[word[i]];
- if(tmp -> children_of_node[c] == NULL) {
- tmp -> children_of_node[c] = new node();
- }
- tmp = tmp -> children_of_node[c];
- }
- tmp -> is_end_of_word = true;
- }
- bool search_word(node * trie, wstring word) {
- node * tmp = trie;
- for(int i = 0; i < (int) word.length(); i++) {
- int c = alphabet[word[i]];
- if(tmp -> children_of_node[c] == NULL) {
- return false;
- }
- tmp = tmp -> children_of_node[c];
- }
- return tmp -> is_end_of_word;
- }
- int main() {
- wifstream file("all.txt");
- file.imbue(locale(file.getloc(), new codecvt_utf8<wchar_t>));
- if(!file.is_open()) {
- cout << "Error opening file!" << endl;
- return 0;
- }
- locale::global(locale("mk_MK.UTF-8"));
- wcout.imbue(locale());
- wcin.imbue(locale());
- node * trie = new node();
- wstring word;
- int r = 0;
- int cnt = 1;
- while(file >> word) {
- for(wchar_t c : word) {
- if(alphabet[c] == 0) {
- alphabet[c] = cnt;
- cnt++;
- }
- }
- add_word(trie, word);
- }
- wstring s;
- wcin >> s;
- cout << s.length() << endl;
- for(wchar_t c : s) {
- cout << alphabet[c] << " ";
- }
- wcout << s << endl;
- if(search_word(trie, s)) {
- wcout << "Ovoj zbor se naoga vo datotekata" << endl;
- }
- else {
- wcout << "Ovoj zbor ne se naoga vo datotekata" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement