The data size is very small, which makes people think of searching. But 10! = 3628800 is not good enough.
Considering that each selection is only related to the previous selection, and N <= 10, you can solve the problem through the State compression dp.
Note: To understand the meaning of a question, you can only specify the maximum number of letters with the same position.
Some preprocessing can be performed before dp.
[Code]
[Cpp]
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <algorithm>
Using namespace std;
Int f [2] [10] [1 <10], g [12] [12], len [10] [10];
String s [10];
Int main ()
{
Int I, j, k, m, n, x, la, lb, p, ans, tmp;
Freopen ("in", "r", stdin );
While (1)
{
Scanf ("% d", & n );
If (n <= 0) break;
Memset (len, 0, sizeof (len ));
For (I = 0; I <n; I ++)
Cin> s [I];
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
If (I! = J)
{
La = s [I]. size ();
Lb = s [j]. size ();
Memset (g, 0, sizeof (g ));
For (k = 0; k <la; k ++)
For (p = 0; p <lb; p ++)
{
Tmp = 0;
For (x = 0; k + x <la & p + x <lb; x ++)
If (s [I] [k + x] = s [j] [p + x]) tmp ++;
Len [I] [j] = max (len [I] [j], tmp );
}
}
M = 1 <n;
X = 0;
Memset (f [x], 255, sizeof (f [x]);
For (I = 0; I <n; I ++)
F [x] [I] [1 <I] = 0;
For (p = 1; p <n; p ++)
{
X ^ = 1;
Memset (f [x], 255, sizeof (f [x]);
For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
If (f [x ^ 1] [I] [j]> = 0)
For (k = 0; k <n; k ++)
If (j> k) & 1) = 0)
F [x] [k] [j | (1 <k)] = max (f [x] [k] [j | (1 <k)], f [x ^ 1] [I] [j] + len [I] [k]);
}
Ans = 0;
For (I = 0; I <n; I ++)
Ans = max (ans, f [x] [I] [s-1]);
Printf ("% d \ n", ans );
}
}