COCI '15 Contest 2 #4 Savez

View as PDF

Submit solution

Points: 15
Time limit: 0.6s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

There are eight planets and one planetoid in the Solar system. It is not a well known fact that there is a secret planet S4 inhabited by small creatures similar to bears, their codename being Lodas. Although this fact is well hidden from the public, the association Savez sent a team lead by general Henrik to study the Lodas. It has been discovered that Lodas have the ability of teleportation and he wants to hire them in his army.

One Lod consists of N strings where the i^{th} string is denoted by x_i. Research has shown that the number of teleportations a Loda can make depends on one special subsequence (not necessarily consecutive) of these strings. Strings x_i and x_j (i < j) can both be in that sequence if and only if string x_j both starts with and ends with string x_i. The number of teleportations a Loda can make is the length of the longest described subsequence.

Determine the number of teleportations.

Input Specification

The first line of input contains the integer N, the number of strings. Each of the following N lines contains one string consisting of uppercase letters of the English alphabet. The input data will be such that there will be less than two million characters in total.

In test cases worth 40% of points, it will hold (1 \le N \le 500).

Output Specification

The first and only line of output must contain the number of teleportations a Loda can make.

Sample Input 1

5
A
B
AA
BBB
AAA

Sample Output 1

3

Explanation for Sample Output 1

Prefix and suffix can intersect so subsequence is A \to AA \to AAA.

Sample Input 2

5
A
ABA
BBB
ABABA
AAAAAB

Sample Output 2

3

Sample Input 3

6
A
B
A
B
A
B

Sample Output 3

3

Explanation for Sample Output 3

Strings in the subsequence are allowed to be equal so subsequence is A \to A \to A or B \to B \to B.


Comments

There are no comments at the moment.