Advertisement
Guest User

1!

a guest
Nov 11th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. //#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
  4. //
  5. //
  6. using namespace std;
  7. //struct trie {
  8. // int value;
  9. // char ch;
  10. // struct trie *sibling; /* Sibling node */
  11. // struct trie *child; /* First child node */
  12. //};
  13. //struct trie *trie_create()
  14. //{
  15. // struct trie *node;
  16. // if ( (node = (trie*)malloc(sizeof(*node))) == NULL)
  17. // return NULL;
  18. // node->ch = '\0';
  19. // node->value = 0;
  20. // node->sibling = NULL;
  21. // node->child = NULL;
  22. // return node;
  23. //}
  24. //struct trie *trie_insert(struct trie *root,
  25. // string key, int value)
  26. //{
  27. // struct trie *node, *parent, *list;
  28. // parent = NULL;
  29. // list = root;
  30. // for (int i=0; key[i] != '\0'; i++) {
  31. ///* Lookup sibling node */
  32. // for (node = list; node != NULL;
  33. // node = node->sibling)
  34. // {
  35. // if (node->ch == key[i])
  36. // break;
  37. // }
  38. // if (node == NULL) {
  39. ///* Node not found. Add new node */
  40. // node = trie_create();
  41. // node->ch = key[i];
  42. // node->sibling = list;
  43. // if (parent != NULL)
  44. // parent->child = node;
  45. // else
  46. // root = node;
  47. // list = NULL;
  48. // } else {
  49. ///* Node found. Move to next level */
  50. // list = node->child;
  51. // }
  52. // parent = node;
  53. // }
  54. ///* Update value in leaf */
  55. //// if (node->value != NULL)
  56. //// free(node->value);
  57. // node->value = value;
  58. // return root;
  59. //}
  60. //
  61. //void trie_print(struct trie *root, int level)
  62. //{
  63. // struct trie *node;
  64. // int i;
  65. // for (node = root; node != NULL;
  66. // node = node->sibling)
  67. // {
  68. // for (i = 0; i < level; i++)
  69. // cout << " ";
  70. // if (node->value != 0)
  71. // cout << node->ch << " " << node->value << endl;
  72. // else
  73. // cout << node->ch << endl;
  74. // if (node->child != NULL)
  75. // trie_print(node->child, level + 1);
  76. // }
  77. //}
  78. //
  79. //int trie_lookup(struct trie *root, char *key)
  80. //{
  81. // struct trie *node, *list;
  82. // for (list = root; *key != '\0'; key++) {
  83. // for (node = list; node != NULL;
  84. // node = node->sibling)
  85. // {
  86. // if (node->ch == *key)
  87. // break;
  88. // }
  89. // if (node != NULL)
  90. // list = node->child;
  91. // else
  92. // return 0;
  93. // }
  94. ///* Check: Node must be a leaf node! */
  95. // return node->value;
  96. //}
  97. //void FreeTree(struct trie *root ) {
  98. // if (root->child) {
  99. //// free(root->value);
  100. // FreeTree(root->child);
  101. // }
  102. // if (root->sibling) {
  103. //// free(root->value);
  104. // FreeTree(root->sibling);
  105. // }
  106. // free(root);
  107. //}
  108. //// Returns 0 if current node has a child
  109. //// If all children are NULL, return 1.
  110. //bool isLastNode(struct trie* root)
  111. //{
  112. //// for (int i = 0; i < ALPHABET_SIZE; i++)
  113. // if (root->child)
  114. // return 0;
  115. // return 1;
  116. //}
  117. //// Recursive function to print auto-suggestions for given
  118. //// node.
  119. //void suggestionsRec(struct trie* root, string currPrefix) {
  120. // // found a string in Trie with the given prefix
  121. // if (root->value == 0) {
  122. // cout << currPrefix;
  123. // cout << endl;
  124. // }
  125. //
  126. // // All children struct node pointers are NULL
  127. // if (isLastNode(root))
  128. // return;
  129. //
  130. //// for (int i = 0; i < ALPHABET_SIZE; i++)
  131. //// {
  132. // if (root->sibling) {
  133. // // append current character to currPrefix string
  134. //// currPrefix.push_back(97 + i);
  135. // currPrefix = currPrefix + root->ch;
  136. //
  137. // // recur over the rest
  138. // suggestionsRec(root->sibling, currPrefix);
  139. // }
  140. // if (root->child) {
  141. // // append current character to currPrefix string
  142. //// currPrefix.push_back(97 + i);
  143. // currPrefix = currPrefix + root->ch;
  144. //
  145. // // recur over the rest
  146. // suggestionsRec(root->child, currPrefix);
  147. // }
  148. //// }
  149. //}
  150. //int FindLastCharacter(struct trie* root, const string query, int level){
  151. // struct trie* pCrawl = root;
  152. // for (int k=level; level < query.length(); level++)
  153. // {
  154. //// int index = CHAR_TO_INDEX(query[level]);
  155. //
  156. // // no string in the Trie has this prefix
  157. // if (pCrawl->ch==query[level]){
  158. // pCrawl=pCrawl->child;
  159. //
  160. // }
  161. //
  162. //// return 0;
  163. //
  164. // pCrawl = pCrawl->children[index];
  165. // }
  166. //}
  167. //// print suggestions for given query prefix.
  168. //int printAutoSuggestions(struct trie* root, const string query)
  169. //{
  170. // struct trie* pCrawl = root;
  171. //
  172. // // Check if prefix is present and find the
  173. // // the node (of last level) with last character
  174. // // of given string.
  175. // int level;
  176. // int n = query.length();
  177. // for (level = 0; level < n; level++)
  178. // {
  179. //// int index = CHAR_TO_INDEX(query[level]);
  180. //
  181. // // no string in the Trie has this prefix
  182. // if (pCrawl->ch==query[level]){
  183. // pCrawl=pCrawl->child;
  184. //
  185. // }
  186. //
  187. //// return 0;
  188. //
  189. // pCrawl = pCrawl->children[index];
  190. // }
  191. //
  192. // // If prefix is present as a word.
  193. // bool isWord = (pCrawl->value != 0);
  194. //
  195. // // If prefix is last node of tree (has no
  196. // // children)
  197. //// bool isLast = isLastNode(pCrawl);
  198. //
  199. // // If prefix is present as a word, but
  200. // // there is no subtree below the last
  201. // // matching node.
  202. // if (isWord)
  203. // {
  204. // cout << query << endl;
  205. // return -1;
  206. // }
  207. //
  208. // // If there are are nodes below last
  209. // // matching character.
  210. // if (!isLast)
  211. // {
  212. // string prefix = query;
  213. // suggestionsRec(pCrawl, prefix);
  214. // return 1;
  215. // }
  216. //}
  217. //int main()
  218. //{
  219. // struct trie *root;
  220. // root = trie_insert(NULL, "bars",60);
  221. // root = trie_insert(root, "baribal", 100);
  222. // root = trie_insert(root, "kit", 3000);
  223. // root = trie_insert(root, "lev", 500);
  224. // root = trie_insert(root, "bars", 70);
  225. // root = trie_insert(root, "kilt", 50);
  226. // int comp = printAutoSuggestions(root, "bar");
  227. //// trie_print(root, 0);
  228. // cout<< "Lookup 'kit':" << trie_lookup(root, (char*)"kit") << endl;
  229. // FreeTree(root);
  230. // return 0;
  231. //}
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253. //#include <bits/stdc++.h>
  254. using namespace std;
  255. struct node{
  256. int flag;
  257. int value;
  258. struct node * child[26];
  259. };
  260. struct NeededNode{
  261. int value;
  262. string y;
  263. };
  264.  
  265. struct node* getnode(){
  266. struct node * tmp = (struct node*)malloc(sizeof(struct node));
  267. tmp->flag=0;
  268. for (int i=0;i<26;i++){
  269. tmp->child[i] = NULL;
  270. tmp->value = 0;
  271. }
  272. return tmp;
  273. }
  274. void addword (struct node *curr,string str, int value){
  275. int c,i;
  276. for (i=0;i<str.length();i++){
  277. c = str[i]-97;
  278. if(curr->child[c]==NULL){
  279. curr->child[c] = getnode();
  280. }
  281. curr = curr->child[c];
  282. }
  283. curr->value=value;
  284. curr->flag = 1;
  285. }
  286. NeededNode* AddNode(NeededNode* Obj,int value, string tmp, int& amount)
  287. {
  288. // NeededNode* Obj;
  289. if (amount == 0)
  290. {
  291. Obj = new NeededNode[amount+1]; // выделение памяти для первой структуры
  292. // Obj[0].value=value;
  293. // Obj[0].y.assign(tmp);
  294. // Obj[0].y=tmp;
  295. }
  296. else
  297. {
  298. NeededNode* tempObj = new NeededNode[amount+1];
  299.  
  300. for (int i = 0; i < amount; i++)
  301. {
  302. tempObj[i] = Obj[i]; // копируем во временный объект
  303. // tempObj[i].y.assign(Obj[i].y);
  304.  
  305.  
  306. }
  307. delete [] Obj;
  308. // tempObj[amount].value=value;
  309. // tempObj[amount].y.assign(tmp);
  310. Obj = tempObj;
  311. // delete [] tempObj;
  312. }
  313. Obj[amount].value=value;
  314. Obj[amount].y=tmp;
  315. return Obj;
  316. }
  317.  
  318. NeededNode* findwords_util(struct node *curr,string tmp, int& amount, NeededNode* &Obj){
  319. int c,i;
  320. if(curr->flag==1){
  321. Obj = AddNode(Obj, curr->value, tmp, amount);
  322. amount++;
  323. // cout <<tmp<<endl;
  324. }
  325. for (i=0;i<26;i++){
  326. if(curr->child[i]!=NULL){
  327. findwords_util(curr->child[i], tmp+(char)(i+'a'), amount, Obj);
  328. }
  329. }
  330. return Obj;
  331. }
  332. NeededNode* findwords(struct node *curr,string str, NeededNode* Obj, int &amount){
  333. string tmp;
  334. int i,c;
  335. for(i=0;i<str.length();i++){
  336. c = str[i]-'a';
  337. tmp += (str[i]);
  338. if(curr->child[c]==NULL){
  339. return NULL;
  340. }
  341. curr = curr->child[c];
  342. }
  343. Obj=findwords_util(curr,tmp, amount, Obj);
  344. return Obj;
  345. }
  346. void freeChildren(struct node *node){
  347. int i;
  348. if ((node != NULL && (node->flag!=1))){//NULL check important only for the root
  349. for (i=0;i<26;i++){
  350. if (node->child[i] != NULL){
  351. freeChildren(node->child[i]);
  352. 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)
  353. }
  354. }
  355. }
  356.  
  357. if (node != NULL){ //again only root could be NULL
  358. free(node);//this has to go first
  359. //node = NULL; this has no meaning, as this is the local variable, not the place you passed it to
  360. }
  361. }
  362.  
  363. void freeTree(struct node *root){
  364. freeChildren(root);
  365. // free(root); this is already done in the freeChildren
  366. // you might want to realloc the root there though, as otherwise you don't have anything allocated to root
  367. root = NULL;
  368. }
  369. NeededNode*& Sort(NeededNode* &Obj, int &amount)
  370. {
  371. int j = 0;
  372. int tmp = 0;
  373. string str;
  374. for(int i=0; i<amount; i++){
  375. j = i;
  376. for(int k = i; k<amount; k++){
  377. if(Obj[j].value<Obj[k].value){
  378. j = k;
  379. }
  380. }
  381. tmp = Obj[i].value;
  382. str = Obj[i].y;
  383. Obj[i].value = Obj[j].value;
  384. Obj[i].y = Obj[j].y;
  385. Obj[j].value = tmp;
  386. Obj[j].y = str;
  387. }
  388. return Obj;
  389. }
  390. //void freeNeededNode(NeededNode* Obj, int amount){
  391. // for (int i=0; i<amount; i++){
  392. // delete(Obj);
  393. // }
  394. // // free(root); this is already done in the freeChildren
  395. // // you might want to realloc the root there though, as otherwise you don't have anything allocated to root
  396. // root = NULL;
  397. //}
  398. int main() {
  399. int n,k,i, value;
  400. int amount=0;
  401.  
  402. struct node *root = getnode();
  403. struct NeededNode* Obj = NULL;
  404. string str;
  405. cin>>n>>k; //num of dict words, num of test words
  406. for (i=0;i<n;i++){
  407. cin>>str;
  408. cin>>value;
  409. addword(root,str, value);
  410. }
  411. // for (i=0;i<k;i++){
  412. cin>>str;
  413. Obj=findwords(root,str, Obj, amount);
  414. // }
  415. Sort(Obj, amount);
  416. for (i=0;i<amount;i++){
  417. cout << Obj[i].y << " " << Obj[i].value << endl;
  418. }
  419. delete [] Obj;
  420. freeTree(root);
  421.  
  422.  
  423. return 0;
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement