Card Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2033 Accepted Submission(s): 874
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.
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
3abbccccb1abcd
Sample Output
60
题意概括:给出n个字符串,这n个字符串可以随意组合,任何前一个字符串,反过来计算和第二个字符串的前缀相同部分最长是多少,然后计算一个得分。
解题思路:
开始没看出来是一个二分匹配的题,看下讨论版才知道原来是KM的一道模板题,如果不看讨论版的话大概一直都不知道是二分匹配类型的题。
代码:
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f
char str[210][1010];
int map[210][210];
int n;
int boy[210],girl[210];
int bb[210],bg[210];
int book[210],dd[210];
int min(int x,int y)
{
if(x>y)
return y;
return x;
}
int find(int x)
{
int i,j;
bg[x]=1;
for(i=0;i<n;i++)
{
if(bb[i]==1)
continue;
int qx=girl[x]+boy[i]-map[x][i];
if(qx==0)
{
bb[i]=1;
if(book[i]==-1||find(book[i]))
{
book[i]=x;
return 1;
}
}
else
{
dd[i]=min(dd[i],qx);
}
}
return 0;
}
void KM()
{
int i,j;
memset(boy,0,sizeof(boy));
memset(book,-1,sizeof(book));
for(i=0;i<n;i++)
{
girl[i]=map[i][0];
for(j=1;j<n;j++)
{
if(girl[i]<map[i][j])
{
girl[i]=map[i][j];
}
}
}
for(i=0;i<n;i++)
{
memset(dd,INF,sizeof(dd));
while(1)
{
memset(bb,0,sizeof(bb));
memset(bg,0,sizeof(bg));
if(find(i))
break;
int d=INF;
for(j=0;j<n;j++)
{
if(bb[j]==0)
d=min(d,dd[j]);
}
for(j=0;j<n;j++)
{
if(bg[j])
girl[j]-=d;
if(bb[j])
boy[j]+=d;
else
dd[j]-=d;
}
}
}
int num=0;
for(i=0;i<n;i++)
{
num+=map[book[i]][i];
}
printf("%d\n",num);
}
int score(char s1[],char s2[])
{
int i,j;
int sum=0;
int l1=strlen(s1);
int l2=strlen(s2);
j=0;
i=l1-1;
while(i>=0&&j<l2)
{
if(s1[i]==s2[j])
sum++;
else
break;
i--;
j++;
}
return sum;
}
int main()
{
int i,j,k;
while(scanf("%d",&n)!=EOF)
{
memset(str,0,sizeof(str));
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
memset(map,0,sizeof(map));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
map[i][j]=0;
else
{
map[i][j]=score(str[i],str[j]);
}
}
}
// for(i=0;i<n;i++)
// {
// for(j=0;j<n;j++)
// {
// printf("%d ",map[i][j]);
// }
// printf("\n");
// }
KM();
}
return 0;
}