abinash_hstu

Linked List Implementation - not much memory efficient.

Oct 21st, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. //Abinash Ghosh(Om)
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cctype>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <climits>
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <vector>
  11. #include <list>
  12. #include <stack>
  13. #include <queue>
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. #include <utility>
  18. #include <sstream>
  19. #include <algorithm>
  20. using  namespace  std;
  21.  
  22. #define PI acos(-1.0)
  23. #define MAX 10000007
  24. #define EPS 1e-9
  25. #define mem(a,b) memset(a,b,sizeof(a))
  26. #define gcd(a,b) __gcd(a,b)
  27. #define pb push_back
  28. #define mp make_pair
  29. #define x first
  30. #define y second
  31. #define Sort(x) sort(x.begin(),x.end())
  32. #define FOR(i, b, e) for(int i = b; i <= e; i++)
  33. #define pr(x) cout<<x<<"\n"
  34. #define pr2(x,y) cout<<x<<" "<<y<<"\n"
  35. #define pr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n";
  36. #define READ(f) freopen(f, "r", stdin)
  37. #define WRITE(f) freopen(f, "w", stdout)
  38.  
  39. typedef  long long ll;
  40. typedef  pair <int, int> pii;
  41. typedef  pair <double , double> pdd;
  42. typedef  pair <ll , ll > pll;
  43. typedef  vector <int> vi;
  44. typedef  vector <pii> vpii;
  45. typedef  vector <ll > vl;
  46.  
  47.  
  48. //int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
  49. //int dx[]={1,1,0,-1,-1,-1,0,1};
  50. //int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
  51. //int dx[]={2,1,-1,-2,-2,-1,1,2};
  52. //int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
  53. // scanf("%d",&n);
  54. #define mxx 27
  55. struct node{
  56.     int next[mxx],val;
  57. }tree[50000];
  58. int last;
  59. void Insert(char *str)
  60. {
  61.     int curr=0;
  62.     for(int i=0;str[i];i++)
  63.     {
  64.         int k=str[i]-'a';
  65.         if(tree[curr].next[k]==-1)
  66.         {
  67.            tree[curr].next[k]=++last;
  68.            mem(tree[last].next,-1);
  69.            tree[last].val=0;
  70.         }
  71.         curr=tree[curr].next[k];
  72.     }
  73.     tree[curr].val++;
  74. }
  75. int Search(char *str)
  76. {
  77.     int curr=0;
  78.     for(int i=0;str[i];i++)
  79.     {
  80.         int k=str[i]-'a';
  81.         if(tree[curr].next[k]==-1)return 0;
  82.         curr=tree[curr].next[k];
  83.     }
  84.     return tree[curr].val;
  85. }
  86. void initial()
  87. {
  88.     last=0;
  89.     mem(tree[0].next,-1);
  90.     tree[0].val=0;
  91. }
  92. int main()
  93. {
  94.     int T,n;
  95.     char s[20];
  96.     scanf("%d",&T);
  97.     FOR(t,1,T)
  98.     {
  99.         initial();
  100.         scanf("%d",&n);
  101.         FOR(i,1,n)
  102.         {
  103.             scanf("%s",s);
  104.             Insert(s);
  105.         }
  106.         scanf("%s",s);
  107.         pr(Search(s));
  108.     }
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment