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
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=150005,maxc=1000005;
int father[maxc],p[maxc][25],Depth[maxc];
void Sparse_Table(int x) { p[x][0]=father[x]; for(int j=1; j<=log2(x); j++) if(p[x][j-1]!=-1)p[x][j]=p[p[x][j-1]][j-1]; } int LCA(int a,int b) { if(Depth[a]<Depth[b])swap(a,b); int k=log2(Depth[a]); for(int i=k; i>=0; i--) { if(Depth[a]==Depth[b])break; if(Depth[a]-(1<<i)>=Depth[b])a=p[a][i]; } if(a==b)return b; for(int i=k; i>=0; i--) if(p[a][i]!=-1&&p[a][i]!=p[b][i]) { a=p[a][i]; b=p[b][i]; } return p[a][0]; }
struct Tree { int child[27]; void clear() { memset(child,0,sizeof(child)); } };
struct Trie { Tree tree[maxc]; int cnt; void init() { cnt=0; memset(tree,0,sizeof(tree)); } int insert(string s) { reverse(s.begin(),s.end()); int x=0; for(int i=0; i<s.length(); i++) { int y=s[i]-'a'; if(tree[x].child[y]==0) { tree[x].child[y]=++cnt; father[cnt]=x; Depth[cnt]=Depth[x]+1; Sparse_Table(cnt); } x=tree[x].child[y]; } return x; } } trie;
int n,m,M[maxn],cnt=0;
int main() { n=Get_Int(); m=Get_Int(); memset(p,-1,sizeof(p)); for(int i=1; i<=n; i++) { char s[10005]; scanf("%s",s); M[++cnt]=trie.insert(s); } for(int i=1; i<=m; i++) { int opt=Get_Int(); if(opt==1) { char s[10005]; scanf("%s",s); M[++cnt]=trie.insert(s); } else { int t=Get_Int(),lca=M[Get_Int()]; for(int i=2; i<=t; i++) { int x=Get_Int(); lca=LCA(lca,M[x]); } printf("%d\n",Depth[lca]); } } return 0; }
|