zoj3034 The Bridges of Kolsberg(DP)

/*
 经典模型:最长公共子序列+字符串处理
*/

ContractedBlock.gif ExpandedBlockStart.gif View Code
 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #define max( a, b ) ((a)>(b)?(a):(b))
6
7 char LeftOS[ 1001 ][ 12 ];
8 char RightOS[ 1001 ][ 12 ];
9 int LeftID[ 1001 ];
10 int LeftV[ 1001 ];
11 int RightID[ 1001 ];
12 int RightV[ 1001 ];
13 char OSList[ 1001 ][ 12 ];
14 int Match[ 1001 ][ 1001 ];
15 int Count[ 1001 ][ 1001 ];
16 int Number = 0;
17
18 int ID( char * Data )
19 {
20 for ( int i = 0 ; i < Number ; ++ i )
21 if ( !strcmp( OSList[ i ], Data ) )
22 return i;
23 strcpy( OSList[ Number ], Data );
24 return Number ++;
25 }
26
27 int main()
28 {
29 int t,n,m;
30 char City[ 12 ];
31 while ( ~scanf("%d",&t) )
32 while ( t -- ) {
33 scanf("%d",&n);
34 for ( int i = 1 ; i <= n ; ++ i )
35 scanf("%s %s %d",City,LeftOS[ i ],&LeftV[ i ]);
36 scanf("%d",&m);
37 for ( int i = 1 ; i <= m ; ++ i )
38 scanf("%s %s %d",City,RightOS[ i ],&RightV[ i ]);
39
40 Number = 0;
41 for ( int i = 1 ; i <= n ; ++ i )
42 LeftID[ i ] = ID( LeftOS[ i ] );
43 for ( int i = 1 ; i <= m ; ++ i )
44 RightID[ i ] = ID( RightOS[ i ] );
45
46 memset( Match, 0, sizeof( Match ) );
47 memset( Count, 0, sizeof( Count ) );
48
49 for ( int i = 1 ; i <= n ; ++ i )
50 for ( int j = 1 ; j <= m ; ++ j ) {
51 if ( Match[ i ][ j ] < Match[ i-1 ][ j ] ) {
52 Match[ i ][ j ] = Match[ i-1 ][ j ];
53 Count[ i ][ j ] = Count[ i-1 ][ j ];
54 }
55 if ( Match[ i ][ j ] < Match[ i ][ j-1 ] ) {
56 Match[ i ][ j ] = Match[ i ][ j-1 ];
57 Count[ i ][ j ] = Count[ i ][ j-1 ];
58 }
59 if ( LeftID[ i ] == RightID[ j ] && Match[ i ][ j ] < Match[ i-1 ][ j-1 ] + LeftV[ i ] + RightV[ j ] ) {
60 Match[ i ][ j ] = Match[ i-1 ][ j-1 ] + LeftV[ i ] + RightV[ j ];
61 Count[ i ][ j ] = Count[ i-1 ][ j-1 ] + 1;
62 }
63 }
64
65 printf("%d %d\n",Match[ n ][ m ],Count[ n ][ m ]);
66 }
67 return 0;
68 }

转载于:https://www.cnblogs.com/-xiaobai-/archive/2011/08/20/2146923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值