The main topic: there are some meow on the meow, each meow has a surname and a name. When you call a name, if you have a MEW name or a string in it, he will meow. Ask each name a few meow meow, and each meow meow a few times.
Train of thought: a very cute question meow ~
The AC automatic mechanism makes the fail tree can be done, but with the SA disorderly time, I am the sa mess of W
Put all the strings (names, inquiries) into a string with $, then do the suffix array, the height array. The process is to record the number of each suffix of which meow, as well as ask in the string starting position. When processing the query, you can quickly access the height array with the Sa,rank array, and for each query, expand to the sides, knowing that the height array is less than the length of the inquiry string. Record the answer when you want to get a time stamp to look forward to heavy (I do not know why the network so much with set, really the constant improper dry food ah. Three times times the time ah.
Of course this is a violent, time complexity is very large, want to get stuck also bullish card. Think of the method of not stuck, but it is more difficult to achieve. Handle the height array with St, and then for each query you can double-click on each side instead of the brute force sweep. After getting a whole range, use the Chairman tree and look directly at how many different meow there are in the range. The second question is not very good to deal with, I have not thought very well qaq
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 300010using namespace Std; int cats,asks,len;int S[max],from[max];int Ask[max],length[max]; int Val[max],sa[max];int Rank[max],height[max]; int ans[max],v[max],t; inline bool Same (int x,int y,int l) {return val[x] = = Val[y] && ((x + l >= len && y + l >= len ) || (x + L < len && y + L < len && val[x + l] = = Val[y + l]));} void Getsuffixarray () {static int _val[max],q[max],cnt[max],lim = 10010; for (int i = 0; i < len; ++i) ++cnt[val[i] = s[i]; for (int i = 1; i < Lim; ++i) cnt[i] + = cnt[i-1]; for (int i = len-1; ~i; i.) sa[--cnt[val[i]] = i; for (int d = 1;; ++d) {int top = 0,l = 1 << (d-1); for (int i = 0; i < len; ++i) if (Sa[i] + l >= len) q[top++] = Sa[i]; for (int i = 0; i < len; ++i) if (Sa[i] >= l) q[top++] = sa[i]-l; for (int i = 0; i < lIm ++i) Cnt[i] = 0; for (int i = 0; i < len; ++i) ++cnt[val[q[i]]; for (int i = 1; i < Lim; ++i) cnt[i] + = cnt[i-1]; for (int i = len-1; ~i; i.) sa[--cnt[val[q[i]]] = q[i]; Lim = 0; for (int i = 0,j, i < Len; ++lim) {for (j = i; j < len-1 && Same (sa[j],sa[j + 1],l); ++j); for (; I <= J; ++i) _val[sa[i] = Lim; } for (int i = 0; i < len; ++i) val[i] = _val[i]; if (lim = = len) break; } return; void GetHeight () {for (int i = 0; i < len; ++i) rank[sa[i]] = i; for (int i = 0,k = 0; i < len; ++i) {if (k)--k; Int j = Sa[rank[i]-1]; while (S[i + K] = = S[j + K]) ++k; Height[rank[i]] = k; }} int main () {cin >> cats >> asks; Len =-1; for (int num,i = 1; I <= cats; ++i) {scanf ("%d", &num); for (int j = 1; j <= num; ++j) scanf ("%d", &s[++len]), from[len] = i; S[++len] = 10021y scanf ("%d", &num); for (int j = 1; j <= num; ++j) scanf ("%d", &s[++len]), from[len] = i; S[++len] = 10001; } for (int i = 1; I <= asks; ++i) {scanf ("%d", &length[i]); Ask[i] = len + 1; for (int j = 1; J <= Length[i]; ++j) scanf ("%d", &s[++len]); S[++len] = 10001; } ++len; Getsuffixarray (); GetHeight (); for (int i = 1; I <= asks; ++i) {int p = rank[ask[i]],temp = 0; ++t; while (Height[p] >= length[i]) {if (From[sa[p-1]]) if (v[from[sa[p-1] []! = T) { V[from[sa[p-1]] = T; ++ans[from[sa[p-1]]; ++temp; }--p; if (!p) break; } p = Rank[ask[i]]; while (Height[p + 1] >= Length[i]) {if (from[sa[p + 1]]) if (v[from[sa[p + 1]]! = T) { V[from[sa[p + 1]] = T; ++ans[from[sa[p + 1]]; ++temp; } ++p; if (p = = len) break; } printf ("%d\n", temp); } for (int i = 1; I <= cats; ++i) printf ("%d%c", ans[i], "\ n" [i = = cats]); return 0;}
Bzoj 2754 scoi 2012 the name suffix array on the Meow planet