Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ull unsigned long long
  6. #define LL long long
  7. #define sl(n) scanf("%lld", &n)
  8. #define sf(n) scanf("%lf", &n)
  9. #define si(n) scanf("%d", &n)
  10. #define sch(n) scanf(" %c", &n)
  11. #define ss(n) scanf("%s", n)
  12. #define pii pair <int, int>
  13. #define pll pair <ll, ll>
  14. #define plp pair <int, pll >
  15. #define pb push_back
  16. #define fr first
  17. #define sc second
  18. #define all(a) a.begin(),a.end()
  19. #define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
  20.  
  21. #define inf (1LL<<50)
  22. #define eps 1e-9
  23.  
  24. ll Set(ll n, ll pos)
  25. {
  26. return n|(1LL<<pos);
  27. }
  28.  
  29. bool Check(ll n, ll pos)
  30. {
  31. return (bool) (n&(1LL<<pos));
  32. }
  33.  
  34. struct node
  35. {
  36. bool ok;
  37. vector<ll>vec;
  38. node *next[26+1];
  39. node()
  40. {
  41. vec.clear();
  42. ok=false;
  43. for(int i=0; i<26; i++) next[i]=NULL;
  44. }
  45. }*root;
  46. void Insert(char *str,int ind)
  47. {
  48. node *curr=root;
  49. for(int i=0; str[i]; i++)
  50. {
  51. int id=str[i]-'a';
  52. if(curr->next[id]==NULL)
  53. curr->next[id]=new node();
  54. curr=curr->next[id];
  55. }
  56. curr->vec.pb(ind);
  57. }
  58. ll Search(char *str, ll lo, ll hi)
  59. {
  60. node *curr=root;
  61. for(int i=0; str[i]; i++)
  62. {
  63. int id=str[i]-'a';
  64. if(curr->next[id]==NULL) return 0;
  65. curr=curr->next[id];
  66. }
  67. if (!(curr->ok))
  68. {
  69. sort(curr->vec.begin(), curr->vec.end());
  70. Unique(curr->vec);
  71. }
  72. curr->ok = true;
  73. return upper_bound(curr->vec.begin(), curr->vec.end(), hi) - lower_bound(curr->vec.begin(), curr->vec.end(), lo);
  74. }
  75. char s[1000010];
  76. char tmp[1000010];
  77. int main ()
  78. {
  79. // freopen("inl.txt", "r", stdin);
  80. // freopen("outu.txt", "w", stdout);
  81. // ios_base::sync_with_stdio(0); // no printf/scanf must be present
  82. ll cs, ts, i, j, k, x, y, z, q, m, n;
  83.  
  84. sl(n);
  85.  
  86. root = new node();
  87.  
  88. k = 0;
  89. while(n--)
  90. {
  91. k++;
  92. ss(s);
  93.  
  94. for (i = 0; s[i]; i++)
  95. {
  96. tmp[0] = 0;
  97. for (j = i; s[j]; j++)
  98. {
  99. char yo[2];
  100. yo[0] = s[j], yo[1] = 0;
  101. strcat(tmp, yo);
  102. Insert(tmp, k);
  103. }
  104. }
  105. }
  106.  
  107. sl(n);
  108. while(n--)
  109. {
  110. sl(x);
  111. sl(y);
  112. ss(s);
  113.  
  114. printf("%lld\n", Search(s, x, y));
  115. }
  116.  
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement