import
java.util.*;
class
Node {
Node[] links =
new
Node[
26
];
int
cnt =
0
;
}
public
class
Main {
static
Node root =
new
Node();
static
void
insert(String word)
{
Node node = root;
for
(
int
i =
0
; i < word.length(); i++) {
if
(node.links[word.charAt(i) -
'a'
] ==
null
) {
node.links[word.charAt(i) -
'a'
]
=
new
Node();
}
node = node.links[word.charAt(i) -
'a'
];
node.cnt++;
}
}
static
void
remove(String word)
{
Node node = root;
for
(
int
i =
0
; i < word.length(); i++) {
node = node.links[word.charAt(i) -
'a'
];
node.cnt--;
}
}
static
int
query(String word)
{
int
cnt =
0
;
Node node = root;
for
(
int
i =
0
; i < word.length(); i++) {
if
(node.links[word.charAt(i) -
'a'
] ==
null
)
break
;
node = node.links[word.charAt(i) -
'a'
];
if
(node.cnt ==
0
)
break
;
cnt++;
}
return
cnt;
}
static
void
findLCP(List<String> A,
int
N)
{
for
(
int
i =
0
; i < N; i++)
insert(A.get(i));
for
(
int
i =
0
; i < N; i++) {
remove(A.get(i));
System.out.print(query(A.get(i)) +
" "
);
insert(A.get(i));
}
System.out.println();
}
public
static
void
main(String[] args)
{
List<String> A1
= Arrays.asList(
"abc"
,
"abb"
,
"aac"
);
int
N1 =
3
;
findLCP(A1, N1);
List<String> A2 = Arrays.asList(
"abracadabra"
,
"bracadabra"
,
"racadabra"
,
"acadabra"
,
"cadabra"
,
"adabra"
,
"dabra"
,
"abra"
,
"bra"
,
"ra"
,
"a"
);
int
N2 =
11
;
findLCP(A2, N2);
}
}