1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 2000006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m , q; int A[MAXN];
vi G[MAXN]; void ade( int u , int v ) {
G[u].pb( v ); }
struct SAM { int son[MAXN][26] , par[MAXN] , len[MAXN]; int lst , cnt; void init( ) { lst = cnt = 1; } void addall( ) { rep( i , 2 , cnt ) ade( par[i] , i ); } void ins( int x ) { int cur = ++ cnt; len[cur] = len[lst] + 1; int p = lst; while( p && !son[p][x] ) son[p][x] = cur , p = par[p]; if( !p ) par[cur] = 1; else { int q = son[p][x]; if( len[q] == len[p] + 1 ) par[cur] = q; else { int ct = ++ cnt; len[ct] = len[p] + 1 , par[ct] = par[q]; memcpy( son[ct] , son[q] , sizeof son[q] ); par[q] = par[cur] = ct; for( ; son[p][x] == q ; p = par[p] ) son[p][x] = ct; } } lst = cur; } } S ;
char s[MAXN] , ch[MAXN]; int brk[MAXN]; int en[MAXN];
pii T[MAXN << 2]; int ls[MAXN << 2] , rs[MAXN << 2] , cnt , rt[MAXN]; void pu( int rt ) { T[rt] = max( T[ls[rt]] , T[rs[rt]] ); } void add( int& rt , int l , int r , int p , int c ) {
if( !rt ) rt = ++ cnt; if( l == r ) { T[rt] = mp( T[rt].fi + c , -l ); return; } int m = l + r >> 1; if( p <= m ) add( ls[rt] , l , m , p , c ); else add( rs[rt] , m + 1 , r , p , c ); pu( rt ); } int merge( int u , int v , int l , int r ) { if( !u || !v ) return u ^ v; int cur = ++ cnt; if( l == r ) { T[cur] = T[u]; T[cur].fi += T[v].fi; return cur; } int m = l + r >> 1; ls[cur] = merge( ls[u] , ls[v] , l , m ) , rs[cur] = merge( rs[u] , rs[v] , m + 1 , r ); T[cur] = max( T[ls[cur]] , T[rs[cur]] ); return cur; } pii que( int rt , int l , int r , int L , int R ) { if( !rt ) return pii( 0 , 0 ); if( L <= l && R >= r ) return T[rt]; int m = l + r >> 1; pii ret(0,0); if( L <= m ) ret = max( ret , que( ls[rt] , l , m , L , R ) ); if( R > m ) ret = max( ret , que( rs[rt] , m + 1 , r , L , R ) ); return ret; }
struct poi { int l , r , idx; }; vector<poi> Q[MAXN]; pii ans[MAXN]; void dfs( int u ) { for( int& v : G[u] ) { dfs( v ); rt[u] = merge( rt[u] , rt[v] , 1 , m ); } for( auto& t : Q[u] ) ans[t.idx] = que( rt[u] , 1 , m , t.l , t.r ); }
int g[MAXN][20]; void afs( int u ) { for( int& v : G[u] ) { g[v][0] = u; rep( k , 1 , 19 ) if( g[g[v][k - 1]][k - 1] ) g[v][k] = g[g[v][k - 1]][k - 1]; else break; afs( v ); } } int getit( int u , int l ) { for( int k = 19 ; k >= 0 ; -- k ) if( S.len[g[u][k]] >= l ) u = g[u][k]; return u; }
int lll[MAXN]; void solve() { S.init(); scanf("%s",s + 1); n = strlen( s + 1 ); rep( i , 1 , n ) S.ins( s[i] - 'a' ); cin >> m; int len; brk[0] = 1; rep( i , 1 , m ) { scanf("%s",ch + brk[i - 1]); len = strlen( ch + brk[i - 1] ); brk[i] = brk[i - 1] + len; S.lst = 1; rep( j , brk[i - 1] , brk[i - 1] + len - 1 ) S.ins( ch[j] - 'a' ); } int cur = 1; rep( i , 1 , n ) cur = S.son[cur][s[i] - 'a'] , en[i] = cur; rep( i , 1 , m ) { cur = 1; rep( j , brk[i - 1] , brk[i] - 1 ) cur = S.son[cur][ch[j] - 'a'] , add( rt[cur] , 1 , m , i , 1 ); } cin >> q; int l , r , L , R , u; S.addall( ); afs( 1 ); rep( i , 1 , q ) { scanf("%d%d%d%d",&l,&r,&L,&R); lll[i] = l; u = getit( en[R] , R - L + 1 ); Q[u].eb( (poi) { l , r , i } ); } dfs( 1 ); rep( i , 1 , q ) { if( !ans[i].se ) printf("%d %d\n",lll[i] , 0); else printf("%d %d\n",-ans[i].se , ans[i].fi); }
}
signed main() {
solve(); return 0; }
|