int main() { int n, m; std::scanf("%d", &n); describe = new describe_t[n]; for(int i = 0; i != n; ++i) { std::scanf("%s", str); add_string(str, describe + i); } make_fail(); std::scanf("%d", &m); for(int i = 0; i != m; ++i) { int a, b, c, d; std::scanf("%d %d %d %d", &a, &b, &c, &d); int u = describe[a - 1][b - 1]; int v = describe[c - 1][d - 1]; int lca = get_lca(u, v); std::printf("%d\n", node[lca].hash); } delete[] describe; return 0; }
int solve() { make_fail(); int tmp=1<<(m); for(int i=0;i<=n;i++) for(int j=0;j<=cnt;j++) for(int s=0;s<tmp;s++) dp[i][j][s]=0; dp[0][0][0]=1; for(int i=0;i<n;i++) for(int j=0;j<=cnt;j++) for(int t=0;t<tmp;t++) { if(dp[i][j][t]==0) continue; for(int v=0;v<26;v++) { int w=node[j].next[v];//自动机的下一个位置 int h=hash[node[j].next[v]];//自动机当前包含的串集合 dp[i+1][w][(t|h)]=(dp[i+1][w][(t|h)]+dp[i][j][t]); if(dp[i+1][w][(t|h)]>=MOD) dp[i+1][w][(t|h)]%=MOD; } } int ans=0; for(int i=0;i<=cnt;i++) for(int j=0;j<tmp;j++) { if(num[j]>=k) ans+=dp[n][i][j]; if(ans>=MOD) ans%=MOD; } return ans; }