Advertisement
Guest User

still mle : /

a guest
Mar 8th, 2013
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  CodeForces
  4. //
  5. //  Created by Giorgi Pataraia on 12/29/12.
  6. //
  7.  
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <string>
  12. #include <vector>
  13. #include <set>
  14. #include <map>
  15. #include <queue>
  16. #include <deque>
  17. #include <algorithm>
  18. #include <cmath>
  19.  
  20. using namespace std;
  21.  
  22. #define fori(i,j,n) for (int i = j; i <= n; i++)
  23. #define ford(i,n,j) for (int i = n; i >= j; i--)
  24. #define every(t) t.begin(),t.end()
  25. #define pb(t) push_back(t)
  26. #define mk(a,b) make_pair(a,b)
  27.  
  28. void openFiles() {
  29.     freopen("input.txt", "r",stdin);
  30.     freopen("output.txt","w",stdout);
  31. }
  32.  
  33. const int maxcharcnt = 30;
  34.  
  35. void print(string &s,int l,int r,bool withSpace) {
  36.     if (withSpace)
  37.         cout<<"  ";
  38.     fori(i,l,r)
  39.         cout<<s[i];
  40.     cout<<endl;
  41. }
  42.  
  43. class node;
  44. class linkedlist;
  45.  
  46. class linkedlist {
  47. public:
  48.     char curchar;
  49.     node * curnode;
  50.     linkedlist * next;
  51.     linkedlist(char c,node * n) {
  52.         curnode = n;
  53.         next = NULL;
  54.         curchar = c;
  55.     }
  56. };
  57.  
  58. class node {
  59. public:
  60.     linkedlist * curlist;
  61.     bool isword;
  62.     node () {
  63.         isword = false;
  64.         curlist = NULL;
  65.     }
  66. };
  67.  
  68. node * find(node * head, char c) {
  69.     linkedlist * curlist = head->curlist;
  70.     while(curlist != NULL) {
  71.         if (curlist->curchar == c)
  72.             return curlist->curnode;
  73.         curlist = curlist->next;
  74.     }
  75.     return NULL;
  76. }
  77.  
  78. void add(linkedlist * head,linkedlist * list) {
  79.     if (head->next == NULL) {
  80.         head->next = list;
  81.     }
  82.     else if (head->curchar <= list->curchar && head->next->curchar >= list->curchar) {
  83.         list->next = head->next;
  84.         head->next = list;
  85.     }
  86.     else {
  87.         add(head->next,list);
  88.     }
  89. }
  90.  
  91. void add(node * head,linkedlist * list) {
  92.     linkedlist * curlist = head->curlist;
  93.     if (curlist == NULL) {
  94.         curlist = list;
  95.     }
  96.     else {
  97.         add(curlist,list);
  98.     }
  99.     head->curlist = curlist;
  100. }
  101.  
  102. void insert(node * head,string &s,unsigned int indexInStr) {
  103.     if (indexInStr == s.size()) {
  104.         head->isword = true;
  105.     }
  106.     else {
  107.         node * nextnode = find(head,s[indexInStr]);
  108.         if (nextnode == NULL) {
  109.             linkedlist * list = new linkedlist(s[indexInStr],new node());
  110.             add(head,list);
  111.             nextnode = list->curnode;
  112.         }
  113.         insert(nextnode,s,indexInStr + 1);
  114.     }
  115. }
  116.  
  117. const int maxFindCount = 20;
  118. int findCount = 0;
  119. string curstr = "00000000000000000000000000000000000";
  120.  
  121. void find(node * head,string &s,unsigned int indexInStr,bool found) {
  122.     if (found) {
  123.         if(head->isword) {
  124.             print(curstr,1,indexInStr-1,true);
  125.             findCount++;
  126.         }
  127.         for (linkedlist * curnode = head->curlist; curnode != NULL; curnode = curnode->next)
  128.             if (findCount < maxFindCount) {
  129.                 curstr[indexInStr] = curnode->curchar;
  130.                 find(curnode->curnode,s,indexInStr + 1,found);
  131.             }
  132.     }
  133.     else {
  134.         if (indexInStr == s.size()) {
  135.             found = true;
  136.             find(head,s,indexInStr,found);
  137.         }
  138.         else {
  139.             curstr[indexInStr] = s[indexInStr];
  140.             node * curnode = find(head,s[indexInStr]);
  141.             if (curnode != NULL)
  142.                 find(curnode,s,indexInStr + 1,found);
  143.         }
  144.     }
  145. }
  146.  
  147. node * root;
  148.  
  149. int main() {
  150.     root = new node();
  151.     openFiles();
  152.     string s = "sun";
  153.     insert(root,s,0);
  154.     while(cin>>s) {
  155.         if(s[0] == '?') {
  156.             findCount = 0;
  157.             print(s,1,s.size()-1,false);
  158.             find(root,s,1,false);
  159.         }
  160.         else {
  161.             insert(root,s,1);
  162.         }
  163.     }
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement