Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define ull unsigned long long
- #define LL long long
- #define sl(n) scanf("%lld", &n)
- #define sf(n) scanf("%lf", &n)
- #define si(n) scanf("%d", &n)
- #define sch(n) scanf(" %c", &n)
- #define ss(n) scanf("%s", n)
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define plp pair <int, pll >
- #define pb push_back
- #define fr first
- #define sc second
- #define all(a) a.begin(),a.end()
- #define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
- #define inf (1LL<<50)
- #define eps 1e-9
- ll Set(ll n, ll pos)
- {
- return n|(1LL<<pos);
- }
- bool Check(ll n, ll pos)
- {
- return (bool) (n&(1LL<<pos));
- }
- struct node
- {
- bool ok;
- vector<ll>vec;
- node *next[26+1];
- node()
- {
- vec.clear();
- ok=false;
- for(int i=0; i<26; i++) next[i]=NULL;
- }
- }*root;
- void Insert(char *str,int ind)
- {
- node *curr=root;
- for(int i=0; str[i]; i++)
- {
- int id=str[i]-'a';
- if(curr->next[id]==NULL)
- curr->next[id]=new node();
- curr=curr->next[id];
- }
- curr->vec.pb(ind);
- }
- ll Search(char *str, ll lo, ll hi)
- {
- node *curr=root;
- for(int i=0; str[i]; i++)
- {
- int id=str[i]-'a';
- if(curr->next[id]==NULL) return 0;
- curr=curr->next[id];
- }
- if (!(curr->ok))
- {
- sort(curr->vec.begin(), curr->vec.end());
- Unique(curr->vec);
- }
- curr->ok = true;
- return upper_bound(curr->vec.begin(), curr->vec.end(), hi) - lower_bound(curr->vec.begin(), curr->vec.end(), lo);
- }
- char s[1000010];
- char tmp[1000010];
- int main ()
- {
- // freopen("inl.txt", "r", stdin);
- // freopen("outu.txt", "w", stdout);
- // ios_base::sync_with_stdio(0); // no printf/scanf must be present
- ll cs, ts, i, j, k, x, y, z, q, m, n;
- sl(n);
- root = new node();
- k = 0;
- while(n--)
- {
- k++;
- ss(s);
- for (i = 0; s[i]; i++)
- {
- tmp[0] = 0;
- for (j = i; s[j]; j++)
- {
- char yo[2];
- yo[0] = s[j], yo[1] = 0;
- strcat(tmp, yo);
- Insert(tmp, k);
- }
- }
- }
- sl(n);
- while(n--)
- {
- sl(x);
- sl(y);
- ss(s);
- printf("%lld\n", Search(s, x, y));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement