zoukankan      html  css  js  c++  java
  • UVALive 5027 二分图 EK

    C - Card Game

    Description

     
    Download as PDF
     

    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$ 
ightarrow$S2,     S2$ 
ightarrow$S3,     S3$ 
ightarrow$S1, the score is 1+3+0 = 4
    2. S1$ 
ightarrow$S2,     S2$ 
ightarrow$S1,     S3$ 
ightarrow$S3, the score is 1+0+0 = 1
    3. S1$ 
ightarrow$S3,     S3$ 
ightarrow$S1,     S2$ 
ightarrow$S2, the score is 0+0+0 = 0
    4. S1$ 
ightarrow$S3,     S3$ 
ightarrow$S2,     S2$ 
ightarrow$S1, the score is 0+3+0 = 3
    5. S1$ 
ightarrow$S1,     S2$ 
ightarrow$S2,     S3$ 
ightarrow$S3, the score is 0+0+0 = 0
    6. S1$ 
ightarrow$S1,     S2$ 
ightarrow$S3,     S3$ 
ightarrow$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$ le$N$ le$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
    


    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    char str[300][1500];
    int lx[300],ly[90000];
    int sx[300],sy[90000];
    const int inf=999999999;
    int w[300][90000];
    int LINK[90000];
    int ans;
    int n;
    
    int dmin;
    bool Find(int u)
    {
        sx[u] =1;
        for(int v = 1;v <= n;v ++)
        {
            if(!sy[v])
            {
                int t = lx[u] + ly[v] - w[u][v];
                if(t)
                {
                    if(dmin > t)
                        dmin = t;
                }
                else
                {
                    sy[v] = true;
                    if(LINK[v] == -1 || Find(LINK[v]))
                    {
                        LINK[v] = u;
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    
    int KM(){
        for(int i = 1;i <= n;i ++)
        {
            for(int j = 1;j <= n;j ++)
                if(lx[i] < w[i][j])
                    lx[i] = w[i][j];
        }
    
        memset(LINK,-1,sizeof(LINK));
    
    
        for(int i = 1;i <= n;i ++)
        {
            while(true)
            {
    
                memset(sx,0,sizeof(sx));
                memset(sy,0,sizeof(sy));
                dmin = inf;
                 if(Find(i))
                    break;
                for(int j = 1;j <= n;j ++)
                {
                    if(sx[j])
                        lx[j] -= dmin;
                    if(sy[j])
                        ly[j] += dmin;
                }
            }
        }
        for(int i = 1;i <= n;i ++)
            ans += w[LINK[i]][i];
       return ans;
    }
    
    
    
    int main(){
    
       while(scanf("%d",&n)!=EOF){
          getchar();
          memset(str,0,sizeof(str));
          memset(lx,0,sizeof(lx));
          memset(ly,0,sizeof(ly));
          memset(w,0,sizeof(w));
          for(int i=1;i<=n;i++){
            scanf("%s",str[i]);
            getchar();
          }
    
          for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
    
                    int num=0;
                if(i==j){
                w[i][j]=0;
                continue;
                }
                int len1=strlen(str[i]);
                int len2=strlen(str[j]);
                int temp1=len1-1,temp2=0;
                while(temp1>=0&&temp2<=len2-1&&str[i][temp1]==str[j][temp2]){
    
                        num++;
                        temp1--;
                        temp2++;
    
                }
                w[i][j]=num;
    
            }
          }
         ans=0;
           ans=KM();
           printf("%d
    ",ans);
       }
       return 0;
    }
  • 相关阅读:
    vue 表格组件分享
    原创-整理了下常用的js数组 、对象、数字、字符串的操作方法
    h5 plus/h5+规范使用,模块索引,教你如何去看h5+的手册
    vue 右键菜单插件 简单、可扩展、样式自定义的右键菜单
    vuecli3 引入script 针对没有cmd amd require等方式的js
    vue新手入门指导,一篇让你学会vue技术栈,本人初学时候的文档
    node搭建本地服务器
    利用nodeJs anywhere搭建本地服务器环境
    vue封装组件的正确方式-封装类似elementui的组件
    微信小程序自定义模态框(字体图标)
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4703531.html
Copyright © 2011-2022 走看看