-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathratina trie.cpp
82 lines (63 loc) · 1.13 KB
/
ratina trie.cpp
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
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <ctime>
using namespace std;
struct node{
node *L[26];
};
typedef node *trie;
trie T;
vector<node*> X[10024];
int i,j,n,m,k;
void baga(trie T,char *S,int x){
if (x) X[i][x-1]=T;
if (!S[x]) return;
if (!T->L[S[x]-'a']){
T->L[S[x]-'a']=new(node);
memset(T->L[S[x]-'a']->L,0,sizeof(T->L));
}
baga(T->L[S[x]-'a'],S,x+1);
}
void readdictionary(){
scanf ("%d%d",&n,&m);
char S[5012];
T=new(node);
memset(T->L,0,sizeof(T->L));
for (i=1;i<=n;i++){
scanf ("%s",&S);
X[i].resize(strlen(S));
baga(T,S,0);
}
}
int sim(int *T,int n){
int cnt=4096;
int i=-1;
for (;cnt;cnt/=2){
if (X[T[1]].size()<=i+cnt) continue;
for (j=2;j<=n;j++)
if (X[T[j]].size()<=i+cnt||X[T[j]][i+cnt]!=X[T[1]][i+cnt]) break;
if (j<=n) continue;
i+=cnt;
}
return i+1;
}
void solve(){
for (i=1;i<=m;i++){
int n;
int T[16];
scanf ("%d",&n);
for (j=1;j<=n;j++)
scanf ("%d",&T[j]);
printf ("%d\n",sim(T,n));
}
}
int main()
{
freopen ("ratina.in","r",stdin);
freopen ("ratina.out","w",stdout);
readdictionary();
solve();
return 0;
}