题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3722
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of sticking two cards is the longest common prefix of the second card and the reverse of the first card. For example, if Jimmy sticks the card S1 containing "abcd" in front of the card S2 containing "dcab", the score is 2. And if Jimmy sticks S2 in front of S1, the score is 0. The card can also stick to itself to form a self-circle, whose score is 0.

For example, there are 3 cards, whose strings are S1="ab", S2="bcc", S3="ccb". There are 6 possible sticking:
1.  S1->S2, S2->S3, S3->S1, the score is 1+3+0 = 4
2.  S1->S2, S2->S1, S3->S3, the score is 1+0+0 = 1
3.  S1->S3, S3->S1, S2->S2, the score is 0+0+0 = 0
4.  S1->S3, S3->S2, S2->S1, the score is 0+3+0 = 3
5.  S1->S1, S2->S2, S3->S3, the score is 0+0+0 = 0
6.  S1->S1, S2->S3, S3->S2, the score is 0+3+3 = 6 
So the best score is 6.

Given the information of all the cards, please help Jimmy find the best possible score.

Input

There are several test cases. The first line of each test case contains an integer N (1 <= N <= 200). Each of the next N lines contains a string Si. You can assume the strings contain alphabets ('a'-'z', 'A'-'Z') only, and the length of every string is no more than 1000. 

Output

Output one line for each test case, indicating the corresponding answer.

Sample Input

3
ab
bcc
ccb
1
abcd

Sample Output

6
0

Problem solving report:

Description: 给定n个字符串,定义字符串两两连接的权值为:s1字符串翻转后匹配s2的最大公共前缀字符个数。自身也算是一个回环,求使得所有串形成一些回环的最大权值。
Problem solving: KM变形题,建二分图时,将每个字符串编号为左右顶点集,边权值就是字符串i和j连接的权值。i和j匹配时,翻转的是前者,并且自身和自身成环时权值始终为0。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 205;
const int inf = 0xffffff0;
char str[MAXN][MAXN * 5];
int link[MAXN], lx[MAXN], ly[MAXN], slack[MAXN];
int n, visx[MAXN], visy[MAXN], map[MAXN][MAXN], len[MAXN];
int Judge(char *s, int l1, char *s2, int l2)
{
    int sum = 0, i = l1 - 1, j = 0;
    while (i >= 0 && j < l2)
    {
        if (s[i] == s2[j])
            sum ++;
        else return sum;
        i--;
        j++;
    }
    return sum;
}
int FindPath(int u)
{
    visx[u] = 1;
    for (int i = 1; i <= n; i++)
    {
        if (visy[i])
            continue;
        int temp = lx[u] + ly[i] - map[u][i];
        if (!temp)
        {
            visy[i] = 1;
            if (link[i] == -1 || FindPath(link[i]))
            {
                link[i] = u;
                return 1;
            }
        }
        else if (slack[i] > temp)
            slack[i] = temp;
    }
    return 0;
}
int KM()
{
    memset(ly, 0, sizeof(ly));
    memset(link, -1, sizeof(link));
    for (int i = 1; i <= n; i++)
    {
        lx[i] = -inf;
        for (int j = 1; j <= n; j++)
            if (map[i][j] > lx[i])
                lx[i] = map[i][j];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            slack[j] = inf;
        while (1)
        {
            memset(visx, 0, sizeof(visx));
            memset(visy, 0, sizeof(visy));
            if (FindPath(i))
                break;
            int d = inf;
            for (int j = 1; j <= n; j++)
                if (!visy[j] && d > slack[j])
                    d = slack[j];
            for (int j = 1; j <= n; j++)
            {
                if (visx[j])
                    lx[j] -= d;
                if (visy[j])
                    ly[j] += d;
                else slack[j] -= d;
            }
        }
    }
    int res = 0;
    for (int i = 1; i <= n; ++i)
        if (link[i] > -1)
            res += map[link[i]][i];
    return res;
}
int main()
{
    while (~scanf("%d", &n))
    {
        memset(map, 0, sizeof(map));
        for (int i = 1; i <= n; i ++)
        {
            scanf("%s", str[i]);
            len[i] = strlen(str[i]);
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (i != j)
                    map[i][j] = Judge(str[i], len[i], str[j], len[j]);
        printf("%d\n", KM());
    }
    return 0;
}