题目描述
输入
输出
样例输入
10
emikuqihgokuhsywlmqemihhpgijkxdukjfmlqlwrpzgwrwozkmlixyxniutssasrriafu
emikuqihgokuookbqaaoyiorpfdetaeduogebnolonaoehthfaypbeiutssasrriafu
emikuqihgokuorocifwwymkcyqevdtglszfzgycbgnpomvlzppwrigowekufjwiiaxniutssasrriafu
emikuqihgokuorociysgfkzpgnotajcfjctjqgjeeiheqrepbpakmlixyxniutssasrriafu
emikuqihgokuorociysgfrhulymdxsqirjrfbngwszuyibuixyxniutssasrriafu
emikuqihgokuorguowwiozcgjetmyokqdrqxzigohiutssasrriafu
emikuqihgokuorociysgsczejjmlbwhandxqwknutzgdmxtiutssasrriafu
emikuqihgokuorociysgvzfcdxdiwdztolopdnboxfvqzfzxtpecxcbrklvtyxniutssasrriafu
emikuqihgokuorocsbtlyuosppxuzkjafbhsayenxsdmkmlixyxniutssasrriafu
emikuqihgokuorociysgfjvaikktsixmhaasbvnsvmkntgmoygfxypktjxjdkliixyxniutssasrriafu
10
emikuqihgokuorociysg yxniutssasrriafu
aiegqmedckgqknky eqpoowonnewbq
xfbdnjbazhdnhkhvb qrqgbnmlltlkkbtyn
bjfhrnfedlhrlolzfv qppxpoofxcr
zhdfpldcbjf stsidponnvnmmdvap
zhdfpldcbjfpjmjxdt gdstsidponnvnmmdvap
dlhjtphgfnjtnqnbhxr wxwmhtsrrzrqqhzet
bjfhrnfedlhrlolzfv frqppxpoofxcr
zhdfpldcbjf dponnvnmmdvap
ucyakgyxweakehes nondykjiiqihhyqvk
样例输出
4
7
3
5
5
1
3
5
10
4
题解
Trie树+可持久化Trie树
一开始都蛋疼的想到树套树了 = =
先对所有字符串建立一棵Trie树。
然后DFS一遍得到的Trie树,按照DFS序将字符串的反串插入到可持久化Trie树中,并记录以每个节点为根的子树中的字符串的范围。
对于每个询问,将在Trie树中找s1,到达目标节点后可以直接拿出它包含字符串的范围,再将s2反过来,根据这个范围在可持久化Trie树中查找有多少个反串以它的反串为前缀(即以它为后缀),得到答案。
注意判无解的情况。
#include <cstdio> #include <cstring> #include <algorithm> #define N 2000010 using namespace std; int next[26][N] , fa[N] , tag[N] , lp[N] , rp[N] , tot = 1 , cnt , root[N] , c[26][N] , si[N] , num , len; char str[N] , ch[N] , ss[N] , ll; void insert(int x , int &y) { int i , j , t = ++num; y = t; for(i = 0 ; i < len ; i ++ ) { for(j = 0 ; j < 26 ; j ++ ) c[j][t] = c[j][x]; c[str[i] - 'a'][t] = ++num , x = c[str[i] - 'a'][x] , t = c[str[i] - 'a'][t] , si[t] = si[x] + 1; } for(i = 0 ; i < 26 ; i ++ ) c[i][t] = c[i][x]; } int query(int x , int y) { int i; for(i = len - 1 ; ~i ; i -- ) x = c[str[i] - 'a'][x] , y = c[str[i] - 'a'][y]; return si[y] - si[x]; } void dfs(int x) { int i; if(tag[x]) { len = 0 , lp[x] = rp[x] = ++cnt; for(i = x ; i != 1 ; i = fa[i]) str[len ++ ] = ch[i]; insert(root[cnt - 1] , root[cnt]); return; } lp[x] = 1 << 30 , rp[x] = -1 << 30; for(i = 0 ; i < 26 ; i ++ ) if(next[i][x]) dfs(next[i][x]) , lp[x] = min(lp[x] , lp[next[i][x]]) , rp[x] = max(rp[x] , rp[next[i][x]]); } void trans(char *s , int ans) { int i , l = strlen(s); for(i = 0 ; i < l ; i ++ ) s[i] = (s[i] - 'a' + ans) % 26 + 'a'; } int main() { int n , m , i , j , l , t , ans = 0; scanf("%d" , &n); for(i = 1 ; i <= n ; i ++ ) { scanf("%s" , str) , l = strlen(str); for(j = 0 , t = 1 ; j < l ; j ++ ) { if(!next[str[j] - 'a'][t]) next[str[j] - 'a'][t] = ++tot , ch[tot] = str[j] , fa[tot] = t; t = next[str[j] - 'a'][t]; } tag[t] = 1; } dfs(1); scanf("%d" , &m); for(i = 1 ; i <= m ; i ++ ) { scanf("%s%s" , ss , str) , trans(ss , ans) , trans(str , ans) , l = strlen(ss) , len = strlen(str); for(j = 0 , t = 1 ; t && j < l ; j ++ ) t = next[ss[j] - 'a'][t]; if(t) printf("%d " , ans = query(root[lp[t] - 1] , root[rp[t]])); else printf("%d " , ans = 0); } return 0; }