http://www.lydsy.com/JudgeOnline/problem.php?id=2754
SA的多模板匹配,复杂度O(len1+log(len2)),len1为查找串长度,len2为模板串长度
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> #include <vector> #include <cstring> #include <stack> #include <cctype> #include <utility> #include <map> #include <string> #include <climits> #include <set> #include <string> #include <sstream> #include <utility> #include <ctime> #include <bitset> using std::priority_queue; using std::vector; using std::swap; using std::stack; using std::sort; using std::max; using std::min; using std::pair; using std::map; using std::string; using std::cin; using std::cout; using std::set; using std::queue; using std::string; using std::stringstream; using std::make_pair; using std::getline; using std::greater; using std::endl; using std::multimap; using std::deque; using std::unique; using std::lower_bound; using std::random_shuffle; using std::bitset; using std::upper_bound; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PAIR; typedef multimap<int, int> MMAP; typedef LL TY; typedef long double LF; const int MAXN(150010); const int MAXM(100010); const int MAXE(100010); const int MAXK(6); const int HSIZE(31313); const int SIGMA_SIZE(26); const int MAXH(19); const int INFI((INT_MAX-1) >> 1); const ULL BASE(31); const LL LIM(10000000); const int INV(-10000); const int MOD(20100403); const double EPS(1e-7); const LF PI(acos(-1.0)); template<typename T> void checkmax(T &a, T b){if(b > a) a = b;} template<typename T> void checkmin(T &a, T b){if(b < a) a = b;} template<typename T> T ABS(const T &a){return a < 0? -a: a;} struct SA { int S[MAXN]; int sa[MAXN], t1[MAXN], t2[MAXN], cnt[MAXN], len, M; void init(int tl, int tm = 128) //tl为原串长度, tm为字符集个数 { len = tl; M = tm; int *p1 = t1; int *p2 = t2; for(int i = 0; i < M; ++i) cnt[i] = 0; for(int i = 0; i <= len; ++i) ++cnt[p1[i] = S[i]]; for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1]; for(int i = len; i >= 0; --i) sa[--cnt[p1[i]]] = i; int temp = 1; for(int k = 1; temp <= len; k <<= 1) { temp = 0; for(int i = len-k+1; i <= len; ++i) p2[temp++] = i; for(int i = 0; i <= len;++i) if(sa[i] >= k) p2[temp++] = sa[i]-k; for(int i = 0; i < M; ++i) cnt[i] = 0; for(int i = 0; i <= len; ++i) ++cnt[p1[p2[i]]]; for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1]; for(int i = len; i >= 0; --i) sa[--cnt[p1[p2[i]]]] = p2[i]; swap(p1, p2); temp = 1; p1[sa[0]] = 0; for(int i = 1; i <= len; ++i) p1[sa[i]] = p2[sa[i-1]] == p2[sa[i]] && p2[sa[i-1]+k] == p2[sa[i]+k]? temp-1: temp++; M = temp; } } int rank[MAXN], height[MAXN]; void getHeight() { int k = 0; for(int i = 0; i <= len; ++i) rank[sa[i]] = i; for(int i = 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 Log[MAXN]; int table[MAXH][MAXN]; void initLog() { Log[0] = -1; for(int i = 1; i < MAXN; ++i) Log[i] = (i&(i-1))? Log[i-1]: Log[i-1]+1; } void initRMQ() { for(int i = 1; i <= len; ++i) table[0][i] = height[i]; for(int i = 1; (1 << i) <= len; ++i) for(int j = 1; j+(1 << i)-1 <= len; ++j) table[i][j] = min(table[i-1][j], table[i-1][j+(1 << (i-1))]); } int lcp(int a, int b) { a = rank[a]; b = rank[b]; if(a > b) swap(a, b); ++a; int temp = Log[b-a+1]; return min(table[temp][a], table[temp][b-(1 << temp)+1]); } int find(int *T, int tlen) //多模板匹配,模板串, 每次查询的复杂度O(tlen+log(len)) { //注意查找前需要初始化,共四个函数 int l = 0, r = len+1, max_match = 0, ans = len; while(l < r) { int mid = (l+r) >> 1; int ts = sa[mid]; int temp = lcp(ts, ans); if(temp < max_match) { if(T[temp] < S[ts+temp]) r = mid; else l = mid+1; } else { while(max_match < tlen && T[max_match] == S[ts+max_match]) ++max_match; if(max_match >= tlen) //匹配完成 return mid; if(T[max_match] < S[ts+max_match]) r = mid; else l = mid+1; ans = ts; } } return -1; //未发现匹配串 } }sa; int hash[MAXN]; int vis[MAXN]; int arr[MAXN]; int count[MAXN]; int main() { int n, m; while(~scanf("%d%d", &n, &m)) { int l1 = 0, l2; int divi = 10001; for(int i = 0; i < n; ++i) { count[i] = 0; vis[i] = -1; } int tn = n*2; for(int i = 0; i < tn; ++i) { if(i) { sa.S[l1] = ++divi; ++l1; } scanf("%d", &l2); for(int j = 0; j < l2; ++j) { scanf("%d", sa.S+l1+j); ++sa.S[l1+j]; hash[l1+j] = i >> 1; } sa.S[l1+l2] = 0; l1 += l2; } sa.init(l1, 50010); sa.getHeight(); sa.initLog(); sa.initRMQ(); for(int i = 0; i < m; ++i) { scanf("%d", &l2); for(int j = 0; j < l2; ++j) { scanf("%d", arr+j); ++arr[j]; } int ind = sa.find(arr, l2); int cnt = 0; if(ind != -1) { int th = hash[sa.sa[ind]]; if(vis[th] != i) { ++count[th]; vis[th] = i; ++cnt; } for(int j = ind; sa.height[j] >= l2; --j) { th = hash[sa.sa[j-1]]; if(vis[th] != i) { ++count[th]; vis[th] = i; ++cnt; } } for(int j = ind+1; sa.height[j] >= l2; ++j) { th = hash[sa.sa[j]]; if(vis[th] != i) { ++count[th]; vis[th] = i; ++cnt; } } } printf("%d\n", cnt); } for(int i = 0; i < n; ++i) { if(i) printf(" "); printf("%d", count[i]); } printf("\n"); } return 0; }