#P9242. [蓝桥杯 2023 省 B] 接龙数列

[蓝桥杯 2023 省 B] 接龙数列

Problem Description

For an integer sequence of length KK: A1,A2,,AKA_{1}, A_{2}, \ldots, A_{K}, we call it a chain sequence if and only if the first digit of AiA_{i} is exactly equal to the last digit of Ai1A_{i-1} (2iK2 \leq i \leq K).

For example, 12,23,35,56,61,1112, 23, 35, 56, 61, 11 is a chain sequence; 12,23,34,5612, 23, 34, 56 is not a chain sequence, because the first digit of 5656 is not equal to the last digit of 3434. All integer sequences of length 11 are chain sequences.

Now you are given a sequence of length NN: A1,A2,,ANA_{1}, A_{2}, \ldots, A_{N}. Please compute the minimum number of elements to delete so that the remaining sequence becomes a chain sequence.

Input Format

The first line contains an integer NN.

The second line contains NN integers A1,A2,,ANA_{1}, A_{2}, \ldots, A_{N}.

Output Format

Output one integer representing the answer.

5
11 121 22 12 2023
1

Hint

Sample Explanation

Delete 2222, and the remaining 11,121,12,202311, 121, 12, 2023 is a chain sequence.

Constraints

For 20%20\% of the testdata, 1N201 \leq N \leq 20.

For 50%50\% of the testdata, 1N1041 \leq N \leq 10^{4}.

For 100%100\% of the testdata, 1N1051 \leq N \leq 10^{5}, 1Ai1091 \leq A_{i} \leq 10^{9}. All AiA_{i} are guaranteed to have no leading zeros.

Lanqiao Cup 2023 Provincial Contest Group B, Problem E.

Translated by ChatGPT 5