最大费用圈覆盖,转换成图的匹配来解之。
/*HDU 3722*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cctype>
using namespace std;
const int maxn = 210;
const int INF = 0x3f3f3f3f;
int n, m;
int W[maxn][maxn];
int Lx[maxn], Ly[maxn];
int Left[maxn];
bool S[maxn], T[maxn];
bool match(int i)
{
S[i] = 1;
for(int j = 1; j <= m; j++) if(Lx[i]+Ly[j] == W[i][j] && !T[j])
{
T[j] = 1;
if(!Left[j] || match(Left[j]))
{
Left[j] = i;
return 1;
}
}
return 0;
}
void update()
{
int a = INF;
for(int i = 1; i <= n; i++) if(S[i])
for(int j = 1; j <= m; j++) if(!T[j])
a = min(a, Lx[i]+Ly[j]-W[i][j]);
for(int i = 1; i <= n; i++)
{
if(S[i]) Lx[i] -= a;
}
for(int j = 1; j <= m; j++)
{
if(T[j]) Ly[j] += a;
}
}
void KM()
{
memset(Left, 0, sizeof(Left));
memset(Lx, 0, sizeof(Lx));
memset(Ly, 0, sizeof(Ly));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
Lx[i] = max(Lx[i], W[i][j]);
}
for(int i = 1; i <= n; i++)
{
for(;;)
{
for(int j = 1; j <= m; j++) S[j] = T[j] = 0;
if(match(i)) break; else update();
}
}
}
inline void readint(int &x)
{
char c;
c = getchar();
while(!isdigit(c)) c = getchar();
x = 0;
while(isdigit(c)) x = x*10+c-'0', c = getchar();
}
inline void writeint(int x)
{
if(x > 9) writeint(x/10);
putchar(x%10+'0');
}
char str[210][1010];
void read_case()
{
char buf[1010];
m = n;
for(int i = 1; i <= n; i++) scanf("%s", str[i]);
}
int cal(char *fir, char *sec)
{
int len1 = strlen(fir), len2 = strlen(sec);
int i, j;
for(i = len1-1, j = 0; i >= 0 && j < len2; i--, j++)
{
if(fir[i] != sec[j]) break;
}
return j;
}
void build()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
W[i][j] = cal(str[i], str[j]);
}
W[i][i] = 0;
}
}
void solve()
{
read_case();
build();
KM();
int ans = 0;
for(int i = 1; i <= m; i++) if(Left[i]) ans += W[Left[i]][i];
printf("%d\n", ans);
}
int main()
{
while(~scanf("%d", &n))
{
solve();
}
return 0;
}