POJ 3049 Invitation Cards ( 快排+深搜)

题意:在一堆字母中找一段字母,使其中至少含有1个原音,2个辅音字母,且按字典序从小到大排列

思路:字母也就是char 类型(小整形),将字符传按一定规则排序,ch - 'a' , (ch 为char类型),即 ‘a' 对应 0 , 'b' 对应1...'z' 对应25...再对字符对应的数字(0...25)排序,然后进行深搜。

//C++	164K	16MS	
#include <stdio.h>
#define N 30
char str[N] ;
int array[N] ;
bool flag[N] ;

int 
Input_Char ( ) 
{
	int index ;
	index = 0 ;
	char ch ;
	while ( ( ch = getchar ( ) ) != '\n' ) 
	{
		if ( ch >= 'a' && ch <= 'z' ) 
		{
			str[index++] = ch ;
		}
	}
	return index ;
}

void 
Char_To_Int ( int const length ) 
{
	for ( int i = 0 ; i < length ; i ++ ) 
	{
		array[i] = str[i] - 'a' ;
	}
	return ;
	
}

void 
Quick_Sort ( int a[] , int const start , int const end ) 
{
	int i , j ;
	i = start ;
	j = end ;
	int temp ;
	temp = a[i] ;
	while ( i < j ) 
	{
		while ( i < j && temp < a[j] ) 
		{
			j -- ;
		}
		if ( i < j ) 
		{
			a[i++] = a[j] ;
		}
		while ( i < j && temp > a[i] ) 
		{
			i ++ ;
		}
		if ( i < j ) 
		{
			a[j--] = a[i] ;
		}
	}
	a[i] = temp ;
	if ( i > start )
	{
		Quick_Sort ( a , start , i ) ;
	}
	if ( i < end ) 
	{
		Quick_Sort ( a , i + 1 , end ) ;
	}
	return ;
}

bool 
Can_Be_Printed ( char barn[] , int const L ) 
{
	int  vowel ;   
	vowel = 0 ;
	for ( int i = 0 ; i < L ; i ++ ) 
	{
		if ( 'a' == barn[i] || 'e' == barn[i] || 'i' == barn[i] || 'o' == barn[i] || 'u' == barn[i] )    
		{
			vowel ++ ;   
		}
	}
	return vowel && L - vowel >= 2 ? true : false ;		// at least has  a vowel and the number of consonant >= 2 
}


void 
dfs ( int const L , int const C , int const array_index , int const barn_index  , char barn[] ) 
{
	if ( L == barn_index ) 
	{
		if ( Can_Be_Printed  ( barn , L ) )  // at least the password is alphabetical then check the other constraint
		{
			for ( int i = 0 ; i < L ; i ++ ) 
			{
				printf ("%c" , barn[i] ) ;
			}
			printf ("\n") ;
		}
		return ;
	}
	else 
	{
		for ( int i = array_index ; i < C ; i ++ ) 
		{
			if ( !flag[i] ) 
			{
				barn[barn_index] = array[i] + 'a' ;
				flag[i] = true ;			//flag
				dfs ( L ,  C , i + 1 , barn_index + 1 , barn ) ;
				flag[i] = false ;			//backtracking 
			}
		}
	}
	return  ;
}

int 
main ( ) 
{
	int L , C ;		//L length , C the number of  lower-case characters
	scanf ("%d%d" , & L , & C ) ;
	getchar ( ) ;		
	int length ;
	length = Input_Char ( ) ;
	Char_To_Int ( length ) ;
	Quick_Sort ( array , 0 , length - 1 ) ;
	char barn[N] ;		//describe the new combination of the password
	dfs ( L , C , 0 , 0 , barn ) ;
	return 0 ;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值