zoukankan      html  css  js  c++  java
  • zoj 3034

    题目:在河两端有两排server,如今要把河两边同样的品牌型号的机器连起来。每一个电脑有个值,

               每一个机器仅仅能与还有一台机器链接。而且不同的链接不交叉,如今要求链接的电脑总之最大。

    分析:dp,最大公共子序列,字符串。还要加一个字符串处理。

    说明:(2011-09-19 11:08)。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define max( a, b ) ((a)>(b)?(a):(b))
    
    char LeftOS[ 1001 ][ 12 ];
    char RightOS[ 1001 ][ 12 ];
    int  LeftID[ 1001 ];
    int  LeftV[ 1001 ];
    int  RightID[ 1001 ];
    int  RightV[ 1001 ];
    char OSList[ 1001 ][ 12 ]; 
    int  Match[ 1001 ][ 1001 ];
    int  Count[ 1001 ][ 1001 ];
    int  Number = 0;
    
    int ID( char * Data ) 
    {
        for ( int i = 0 ; i < Number ; ++ i )
            if ( !strcmp( OSList[ i ], Data ) )
                return i;
        strcpy( OSList[ Number ], Data );
        return Number ++;
    }
    
    int main()
    {
        int  t,n,m;
        char City[ 12 ];
        while ( ~scanf("%d",&t) ) 
        while ( t -- ) {
            scanf("%d",&n);
            for ( int i = 1 ; i <= n ; ++ i )
                scanf("%s %s %d",City,LeftOS[ i ],&LeftV[ i ]);
            scanf("%d",&m);
            for ( int i = 1 ; i <= m ; ++ i )
                scanf("%s %s %d",City,RightOS[ i ],&RightV[ i ]);
            
            Number = 0;
            for ( int i = 1 ; i <= n ; ++ i )
                LeftID[ i ] = ID( LeftOS[ i ] );
            for ( int i = 1 ; i <= m ; ++ i )
                RightID[ i ] = ID( RightOS[ i ] );
            
            memset( Match, 0, sizeof( Match ) );
            memset( Count, 0, sizeof( Count ) );
            
            for ( int i = 1 ; i <= n ; ++ i )
            for ( int j = 1 ; j <= m ; ++ j ) {
                if ( Match[ i ][ j ] < Match[ i-1 ][ j ] ) {
                    Match[ i ][ j ] = Match[ i-1 ][ j ];
                    Count[ i ][ j ] = Count[ i-1 ][ j ];
                }
                if ( Match[ i ][ j ] < Match[ i ][ j-1 ] ) {
                    Match[ i ][ j ] = Match[ i ][ j-1 ];
                    Count[ i ][ j ] = Count[ i ][ j-1 ];
                }
                if ( LeftID[ i ] == RightID[ j ] && Match[ i ][ j ] < Match[ i-1 ][ j-1 ] + LeftV[ i ] + RightV[ j ] ) {
                    Match[ i ][ j ] = Match[ i-1 ][ j-1 ] + LeftV[ i ] + RightV[ j ];
                    Count[ i ][ j ] = Count[ i-1 ][ j-1 ] + 1;
                }
            }
    
            printf("%d %d
    ",Match[ n ][ m ],Count[ n ][ m ]);
        }
        return 0;
    }

  • 相关阅读:
    Spring JPA使用CriteriaBuilder动态构造查询
    vscode 将本地项目上传到github、从github克隆项目以及删除github上的某个文件夹
    CDN 加速配置
    dos常用命令
    使用Github作为博客的图床
    一个简单mock-server 解决方案
    postman(三):详解postman动态变量使用
    postman(一):详解在postman中使用环境变量
    postman(二):详解在Pre-request Script中如何执行请求
    MySql中4种批量更新的方法
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6940646.html
Copyright © 2011-2022 走看看