匹配情况hdu4451-Dressing
最近应用开发的过程中出现了一个小问题,顺便记录一下原因和方法--匹配情况
http://acm.hdu.edu.cn/showproblem.php?pid=4451
以下用了两种思路求解, 另外用了数组用set两种写法,3份代码,另外还有一种思路的数组方法没有写,感兴趣的朋友们可以尝试下;
A;
应用两个数组,左边存和裤子不匹配的情况,右边统计鞋子和裤子不匹配的情况,然后求解每项的 ( n - lw[ i ] ) * ( k - rw[ i ] ) 的总和便可 ;
B :
应用两个数组,左边存和裤子不匹配的情况,右边统计鞋子和裤子不匹配的情况,然后求解总的情况, 减去和裤子不匹配的衣服 和减去和鞋子不匹配的鞋子数量,在加上减了两次的 ,既不匹配衣服又不匹配鞋子的情况 ,详细公式 如下: X 代表 满足左边数量(衣服) ,Y 代表裤子 ,Z代表鞋子;
所以有 N* M * K - X * Y - Y * Z + X * Z ;
// File Name: hdu4451shuzu.c
// Author: bo_jwolf
// Created Time: 2013年05月24日 星期五 16:12:38
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
int main()
{
freopen("cin.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n , m , k , t , a , b ;
int lw[ 10005 ] , rw[ 10005 ] ;
char str1[ 20 ] , str2[ 20 ] ;
while( scanf( "%d%d%d" , &n , &m , &k ) )
{
if( n == 0 && m == 0 && k == 0 )
break ;
memset( lw , 0 ,sizeof( lw ) ) ;
memset( rw , 0 ,sizeof( rw ) ) ;
scanf( "%d" , &t );
while( t-- )
{
scanf( "%s %d %s %d" , str1 , &a , str2 , &b ) ;
if( str1[ 0 ] == 'c' )
{
lw[ b ]++ ;
}
if( str1[ 0 ] == 'p' )
{
rw[ a ]++ ;
}
}
int ans = 0 ;
for(int i = 1 ; i <= m ; i++ )
{
ans += (n - lw[ i ] )*(k - rw[ i ] );
}
printf( "%d\n" , ans );
}
return 0 ;
}
// File Name: hdu4451.c
// Author: bo_jwolf
// Created Time: 2013年05月24日 星期五 14:52:33
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<iostream>
#include<set>
using namespace std ;
const int maxn = 1005 ;
int main()
{
set<int> lw[ maxn ] ;
set<int> rw[ maxn ] ;
// freopen("cin.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n , m , k , t ,a,b;
char str1[ 100 ] , str2[ 100 ] ;
while( scanf("%d%d%d" ,&n , &m , &k ) != EOF && (n | m | k ) )
{
for(int i = 0 ; i <= m ;i++ )
{
lw[ i ].clear() ;
rw[ i ].clear();
// cout<< lw[i].size() << rw[i].size() <<"bbbbbbbbbbbb\n";
}
scanf( "%d" , &t );
while( t-- )
{
scanf( "%s %d %s %d" ,str1 , &a , str2 , &b ) ;
if(! strcmp( str1 ,"clothes") )
{
if( lw[ b ] .find( a ) == lw[ b ].end() )
lw[ b ].insert( a ) ;
}
else
// if( !strcmp( str2 , "shoes" ) )
{
if( rw[ a ].find( b ) == rw[ a ].end())
rw[ a ].insert( b ) ;
}
/* else
while(true)
;*/
}
int ans = 0 ;
for( int i = 1 ; i <= m ; i++ )
{/*
printf( "%d\n" ,n - lw[ i ].size() );
printf( "%d\n" ,m -rw[i ].size() );
printf( "%daaaaaaaaaaaaaaa\n" , ans ) ;
*/ans += ( n - lw[ i ].size() )* ( k - rw[ i ].size() );
//cout << ans << endl;
}
printf( "%d\n" , ans ) ;
}
return 0 ;
}
// File Name: hdu4451.c
// Author: bo_jwolf
// Created Time: 2013年05月24日 星期五 14:52:33
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<iostream>
#include<set>
using namespace std ;
const int maxn = 1005 ;
int main()
{
set<int> lw[ maxn ] ;
set<int> rw[ maxn ] ;
freopen("cin.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n , m , k , t ,a,b;
char str1[ 100 ] , str2[ 100 ] ;
while( scanf("%d%d%d" ,&n , &m , &k ) != EOF && (n | m | k ) )
{
for(int i = 1 ; i <= m ;i++ )
{
lw[ i ].clear() ;
rw[ i ].clear();
// cout<< lw[i].size() << rw[i].size() <<"bbbbbbbbbbbb\n";
}
scanf( "%d" , &t );
while( t-- )
{
scanf( "%s %d %s %d" ,str1 , &a , str2 , &b ) ;
if(! strcmp( str1 ,"clothes") )
{
if( lw[ b ] .find( a ) == lw[ b ].end() )
lw[ b ].insert( a ) ;
}
else
// if( !strcmp( str2 , "shoes" ) )
{
if( rw[ a ].find( b ) == rw[ a ].end())
rw[ a ].insert( b ) ;
}
/* else
while(true)
;*/
}
int ans = n * m * k;
for( int i = 1 ; i <= m ; i++ )
{/*
printf( "%d\n" ,n - lw[ i ].size() );
printf( "%d\n" ,m -rw[i ].size() );
printf( "%daaaaaaaaaaaaaaa\n" , ans ) ;
*///ans += ( n - lw[ i ].size() )* ( k - rw[ i ].size() );
//cout << ans << endl;
const int x = lw[i].size();
const int y = rw[i].size();
ans = ans - x * k - y * n + x * y;
}
printf( "%d\n" , ans ) ;
}
return 0 ;
}
文章结束给大家分享下程序员的一些笑话语录: 这个世界上只有10种人:懂得二进制的和不懂得二进制的。
---------------------------------
原创文章 By
匹配和情况
---------------------------------

浙公网安备 33010602011771号