HDU3722 Card Game 二分圖之最優匹配 KM演算法

來源:互聯網
上載者:User

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

題意就是說給你n個字串,任意兩個字串進行互相拼接;例如s1="abcd",s2="dbca",將s1拼接在s2的後面,所得到的值就是將s1翻轉得到s1'="dcba",s1'和s2共有的首碼為1;其值就是1;現在要求的就是講這些拼起來得到的值為最大。將字串分成X集合和Y集合,每個集合均有n個字串,任意兩個字串得到的值作為其權值,最後利用簡單的KM就能實現

 

代碼:

#include<iostream>
using namespace std;
#define MAX 201
#define MAXN 0xffffff
int map[MAX][MAX],link[MAX],dx[MAX],dy[MAX];
bool visx[MAX],visy[MAX];
int n,lack;
char A[201][10001];
bool DFS(int v)
{
 visx[v]=true;
 for(int i=1;i<=n;i++)
 {
  if(visy[i])
   continue;
  int t=dx[v]+dy[i]-map[v][i];
  if(!t)
  {
   visy[i]=true;
   if(link[i]==-1||DFS(link[i]))
   {
    link[i]=v;
    return true;
   }
  }
  else
  {
   if(t<lack)
    lack=t;
  }
 }
 return false;
}
void KM()
{
 int i,j;
 memset(dx,0,sizeof(dx));
 memset(dy,0,sizeof(dy));
 memset(link,-1,sizeof(link));
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   if(map[i][j]>dx[i])
    dx[i]=map[i][j];
 }
 for(i=1;i<=n;i++)
 {
  while(true)
  {
   memset(visx,0,sizeof(visx));
   memset(visy,0,sizeof(visy));
   lack=MAXN;
   if(DFS(i))
    break;
   for(j=1;j<=n;j++)
   {
    if(visx[j])
     dx[j]-=lack;
    if(visy[j])
     dy[j]+=lack;
   }
   
  }
 }
 int ans=0;
 for(i=1;i<=n;i++)
  ans+=map[link[i]][i];
    cout<<ans<<endl;
}
void Q()
{
 int i,j,p,q;
 for(i=1;i<=n;i++)
 {
  int len1=strlen(A[i]);
  for(j=1;j<=n;j++)
  {
   if(i==j)
    map[i][j]=0;
   else
   {
    int len2=strlen(A[j]);
    q=0;
    p=len2-1;
    while(q<len1&&p>=0&&A[i][q]==A[j][p])
    {
     q++;p--;
    }
    map[i][j]=q;
   }
  }
 }
}
int main()
{
 while(cin>>n)
 {
  int i,j;
  for(i=1;i<=n;i++)
  {
   for(j=1;j<=n;j++)
    map[i][j]=0;
  }
  for(i=1;i<=n;i++)
   scanf("%s",A[i]);
  Q();
  KM();
 }
 return 0;
}

咱能力有限,代碼寫的不漂亮,如有瑕疵,請評論指教!!!!!!!!!!!!!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.