Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- //#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
- //
- //
- using namespace std;
- //struct trie {
- // int value;
- // char ch;
- // struct trie *sibling; /* Sibling node */
- // struct trie *child; /* First child node */
- //};
- //struct trie *trie_create()
- //{
- // struct trie *node;
- // if ( (node = (trie*)malloc(sizeof(*node))) == NULL)
- // return NULL;
- // node->ch = '\0';
- // node->value = 0;
- // node->sibling = NULL;
- // node->child = NULL;
- // return node;
- //}
- //struct trie *trie_insert(struct trie *root,
- // string key, int value)
- //{
- // struct trie *node, *parent, *list;
- // parent = NULL;
- // list = root;
- // for (int i=0; key[i] != '\0'; i++) {
- ///* Lookup sibling node */
- // for (node = list; node != NULL;
- // node = node->sibling)
- // {
- // if (node->ch == key[i])
- // break;
- // }
- // if (node == NULL) {
- ///* Node not found. Add new node */
- // node = trie_create();
- // node->ch = key[i];
- // node->sibling = list;
- // if (parent != NULL)
- // parent->child = node;
- // else
- // root = node;
- // list = NULL;
- // } else {
- ///* Node found. Move to next level */
- // list = node->child;
- // }
- // parent = node;
- // }
- ///* Update value in leaf */
- //// if (node->value != NULL)
- //// free(node->value);
- // node->value = value;
- // return root;
- //}
- //
- //void trie_print(struct trie *root, int level)
- //{
- // struct trie *node;
- // int i;
- // for (node = root; node != NULL;
- // node = node->sibling)
- // {
- // for (i = 0; i < level; i++)
- // cout << " ";
- // if (node->value != 0)
- // cout << node->ch << " " << node->value << endl;
- // else
- // cout << node->ch << endl;
- // if (node->child != NULL)
- // trie_print(node->child, level + 1);
- // }
- //}
- //
- //int trie_lookup(struct trie *root, char *key)
- //{
- // struct trie *node, *list;
- // for (list = root; *key != '\0'; key++) {
- // for (node = list; node != NULL;
- // node = node->sibling)
- // {
- // if (node->ch == *key)
- // break;
- // }
- // if (node != NULL)
- // list = node->child;
- // else
- // return 0;
- // }
- ///* Check: Node must be a leaf node! */
- // return node->value;
- //}
- //void FreeTree(struct trie *root ) {
- // if (root->child) {
- //// free(root->value);
- // FreeTree(root->child);
- // }
- // if (root->sibling) {
- //// free(root->value);
- // FreeTree(root->sibling);
- // }
- // free(root);
- //}
- //// Returns 0 if current node has a child
- //// If all children are NULL, return 1.
- //bool isLastNode(struct trie* root)
- //{
- //// for (int i = 0; i < ALPHABET_SIZE; i++)
- // if (root->child)
- // return 0;
- // return 1;
- //}
- //// Recursive function to print auto-suggestions for given
- //// node.
- //void suggestionsRec(struct trie* root, string currPrefix) {
- // // found a string in Trie with the given prefix
- // if (root->value == 0) {
- // cout << currPrefix;
- // cout << endl;
- // }
- //
- // // All children struct node pointers are NULL
- // if (isLastNode(root))
- // return;
- //
- //// for (int i = 0; i < ALPHABET_SIZE; i++)
- //// {
- // if (root->sibling) {
- // // append current character to currPrefix string
- //// currPrefix.push_back(97 + i);
- // currPrefix = currPrefix + root->ch;
- //
- // // recur over the rest
- // suggestionsRec(root->sibling, currPrefix);
- // }
- // if (root->child) {
- // // append current character to currPrefix string
- //// currPrefix.push_back(97 + i);
- // currPrefix = currPrefix + root->ch;
- //
- // // recur over the rest
- // suggestionsRec(root->child, currPrefix);
- // }
- //// }
- //}
- //int FindLastCharacter(struct trie* root, const string query, int level){
- // struct trie* pCrawl = root;
- // for (int k=level; level < query.length(); level++)
- // {
- //// int index = CHAR_TO_INDEX(query[level]);
- //
- // // no string in the Trie has this prefix
- // if (pCrawl->ch==query[level]){
- // pCrawl=pCrawl->child;
- //
- // }
- //
- //// return 0;
- //
- // pCrawl = pCrawl->children[index];
- // }
- //}
- //// print suggestions for given query prefix.
- //int printAutoSuggestions(struct trie* root, const string query)
- //{
- // struct trie* pCrawl = root;
- //
- // // Check if prefix is present and find the
- // // the node (of last level) with last character
- // // of given string.
- // int level;
- // int n = query.length();
- // for (level = 0; level < n; level++)
- // {
- //// int index = CHAR_TO_INDEX(query[level]);
- //
- // // no string in the Trie has this prefix
- // if (pCrawl->ch==query[level]){
- // pCrawl=pCrawl->child;
- //
- // }
- //
- //// return 0;
- //
- // pCrawl = pCrawl->children[index];
- // }
- //
- // // If prefix is present as a word.
- // bool isWord = (pCrawl->value != 0);
- //
- // // If prefix is last node of tree (has no
- // // children)
- //// bool isLast = isLastNode(pCrawl);
- //
- // // If prefix is present as a word, but
- // // there is no subtree below the last
- // // matching node.
- // if (isWord)
- // {
- // cout << query << endl;
- // return -1;
- // }
- //
- // // If there are are nodes below last
- // // matching character.
- // if (!isLast)
- // {
- // string prefix = query;
- // suggestionsRec(pCrawl, prefix);
- // return 1;
- // }
- //}
- //int main()
- //{
- // struct trie *root;
- // root = trie_insert(NULL, "bars",60);
- // root = trie_insert(root, "baribal", 100);
- // root = trie_insert(root, "kit", 3000);
- // root = trie_insert(root, "lev", 500);
- // root = trie_insert(root, "bars", 70);
- // root = trie_insert(root, "kilt", 50);
- // int comp = printAutoSuggestions(root, "bar");
- //// trie_print(root, 0);
- // cout<< "Lookup 'kit':" << trie_lookup(root, (char*)"kit") << endl;
- // FreeTree(root);
- // return 0;
- //}
- //#include <bits/stdc++.h>
- using namespace std;
- struct node{
- int flag;
- int value;
- struct node * child[26];
- };
- struct NeededNode{
- int value;
- string y;
- };
- struct node* getnode(){
- struct node * tmp = (struct node*)malloc(sizeof(struct node));
- tmp->flag=0;
- for (int i=0;i<26;i++){
- tmp->child[i] = NULL;
- tmp->value = 0;
- }
- return tmp;
- }
- void addword (struct node *curr,string str, int value){
- int c,i;
- for (i=0;i<str.length();i++){
- c = str[i]-97;
- if(curr->child[c]==NULL){
- curr->child[c] = getnode();
- }
- curr = curr->child[c];
- }
- curr->value=value;
- curr->flag = 1;
- }
- NeededNode* AddNode(NeededNode* Obj,int value, string tmp, int& amount)
- {
- // NeededNode* Obj;
- if (amount == 0)
- {
- Obj = new NeededNode[amount+1]; // выделение памяти для первой структуры
- // Obj[0].value=value;
- // Obj[0].y.assign(tmp);
- // Obj[0].y=tmp;
- }
- else
- {
- NeededNode* tempObj = new NeededNode[amount+1];
- for (int i = 0; i < amount; i++)
- {
- tempObj[i] = Obj[i]; // копируем во временный объект
- // tempObj[i].y.assign(Obj[i].y);
- }
- delete [] Obj;
- // tempObj[amount].value=value;
- // tempObj[amount].y.assign(tmp);
- Obj = tempObj;
- // delete [] tempObj;
- }
- Obj[amount].value=value;
- Obj[amount].y=tmp;
- return Obj;
- }
- NeededNode* findwords_util(struct node *curr,string tmp, int& amount, NeededNode* &Obj){
- int c,i;
- if(curr->flag==1){
- Obj = AddNode(Obj, curr->value, tmp, amount);
- amount++;
- // cout <<tmp<<endl;
- }
- for (i=0;i<26;i++){
- if(curr->child[i]!=NULL){
- findwords_util(curr->child[i], tmp+(char)(i+'a'), amount, Obj);
- }
- }
- return Obj;
- }
- NeededNode* findwords(struct node *curr,string str, NeededNode* Obj, int &amount){
- string tmp;
- int i,c;
- for(i=0;i<str.length();i++){
- c = str[i]-'a';
- tmp += (str[i]);
- if(curr->child[c]==NULL){
- return NULL;
- }
- curr = curr->child[c];
- }
- Obj=findwords_util(curr,tmp, amount, Obj);
- return Obj;
- }
- void freeChildren(struct node *node){
- int i;
- if ((node != NULL && (node->flag!=1))){//NULL check important only for the root
- for (i=0;i<26;i++){
- if (node->child[i] != NULL){
- freeChildren(node->child[i]);
- node->child[i] = NULL; //you have to remove the address, otherwise it stays, just is no longer allocated (but it is still possible to access it, though it might crash or do whatever else)
- }
- }
- }
- if (node != NULL){ //again only root could be NULL
- free(node);//this has to go first
- //node = NULL; this has no meaning, as this is the local variable, not the place you passed it to
- }
- }
- void freeTree(struct node *root){
- freeChildren(root);
- // free(root); this is already done in the freeChildren
- // you might want to realloc the root there though, as otherwise you don't have anything allocated to root
- root = NULL;
- }
- NeededNode*& Sort(NeededNode* &Obj, int &amount)
- {
- int j = 0;
- int tmp = 0;
- string str;
- for(int i=0; i<amount; i++){
- j = i;
- for(int k = i; k<amount; k++){
- if(Obj[j].value<Obj[k].value){
- j = k;
- }
- }
- tmp = Obj[i].value;
- str = Obj[i].y;
- Obj[i].value = Obj[j].value;
- Obj[i].y = Obj[j].y;
- Obj[j].value = tmp;
- Obj[j].y = str;
- }
- return Obj;
- }
- //void freeNeededNode(NeededNode* Obj, int amount){
- // for (int i=0; i<amount; i++){
- // delete(Obj);
- // }
- // // free(root); this is already done in the freeChildren
- // // you might want to realloc the root there though, as otherwise you don't have anything allocated to root
- // root = NULL;
- //}
- int main() {
- int n,k,i, value;
- int amount=0;
- struct node *root = getnode();
- struct NeededNode* Obj = NULL;
- string str;
- cin>>n>>k; //num of dict words, num of test words
- for (i=0;i<n;i++){
- cin>>str;
- cin>>value;
- addword(root,str, value);
- }
- // for (i=0;i<k;i++){
- cin>>str;
- Obj=findwords(root,str, Obj, amount);
- // }
- Sort(Obj, amount);
- for (i=0;i<amount;i++){
- cout << Obj[i].y << " " << Obj[i].value << endl;
- }
- delete [] Obj;
- freeTree(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement