#include<cstdio>
#include<algorithm>
#include<ctype.h>
#include<vector>
#include<cstring>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 3500005;
ll ans, n, all;
char s[N]; int Q;
struct SuffixAutomaton{
vector<int> g[N], v;
ll dep[N], sz[N], dd[N];
int ch[N][26], fa[N], tail, size;
inline SuffixAutomaton(){ size = tail = 1; }
inline int newnode(int x){ return dep[++size] = x, size; }
inline void ins(int c, int ff){
int p = tail, np = newnode(dep[p] + 1);
if(!ff) sz[np] = 1; else v.push_back(np);
for(; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
if(!p) return (void) (fa[np] = 1, tail = np);
int q = ch[p][c];
if(dep[q] == dep[p] + 1) fa[np] = q;
else{
int nq = newnode(dep[p] + 1);
fa[nq] = fa[q], fa[np] = fa[q] = nq;
if(ff == 1) sz[nq] = sz[q];
for(int i = 0; i < 26; i++) ch[nq][i] = ch[q][i];
for(; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
}tail = np;
}
inline void addedge(){
for(int i = 2; i <= size; i++) g[fa[i]].push_back(i);
}
inline void dfs(int u){
for(int i = 0; i < g[u].size(); i++)
dfs(g[u][i]), sz[u] += sz[g[u][i]];
}
inline ll calc(char *s){
tail = 1;
v.clear(); ll len = strlen(s), ans = 0, all = 0;
for(int i = 0; i < len; i++) ins(s[i] - 'a', 1);
for(int i = 0; i < v.size(); i++){
int u = v[i];
for(int p = u; p; p = fa[p]) {
if(dd[p]) break;
if(sz[p]) ans += dep[p] - dep[fa[p]];
all += dep[p] - dep[fa[p]], dd[p] = 1;
}
}
for(int i = 0; i < v.size(); i++){
int u = v[i];
for(int p = u; p; p = fa[p]){
if(!dd[p]) break; dd[p] = 0;
}
}
return all - ans;
}
}van;
int main(){
scanf("%s", s); n = strlen(s), read(Q);
for(int i = 0; i < n; i++) van.ins(s[i] - 'a', 0);
van.addedge(), van.dfs(1);
while(Q--){
int l, r;
scanf("%s", s), read(l), read(r);
printf("%lld\n", van.calc(s));
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 17.967 ms | 87 MB + 640 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 17.787 ms | 87 MB + 844 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 18.061 ms | 88 MB + 124 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 134.641 ms | 178 MB + 280 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 129.663 ms | 175 MB + 416 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 219.259 ms | 292 MB + 196 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 218.276 ms | 292 MB + 212 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 116.143 ms | 132 MB + 308 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 53.629 ms | 129 MB + 68 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 250.039 ms | 181 MB + 484 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 118.255 ms | 181 MB + 852 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 442.5 ms | 233 MB + 712 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 198.606 ms | 234 MB + 428 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 607.493 ms | 286 MB + 596 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 289.75 ms | 285 MB + 640 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 772.168 ms | 339 MB + 800 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 387.214 ms | 334 MB + 716 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 629.15 ms | 282 MB + 24 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #19 | 678.748 ms | 300 MB + 916 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #20 | 729.17 ms | 319 MB + 980 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #21 | 765.161 ms | 339 MB + 764 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #22 | 779.964 ms | 339 MB + 968 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #23 | 767.797 ms | 339 MB + 508 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #24 | 770.141 ms | 340 MB + 68 KB | Wrong Answer | Score: 0 | 显示更多 |
Testcase #25 | 771.15 ms | 339 MB + 352 KB | Wrong Answer | Score: 0 | 显示更多 |