POJ 3049 Invitation Cards ( 快排+深搜)

来源:互联网 发布:微信刷屏软件 编辑:程序博客网 时间:2024/06/10 03:11

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

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

//C++164K16MS#include <stdio.h>#define N 30char 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 ;//flagdfs ( 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 charactersscanf ("%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 passworddfs ( L , C , 0 , 0 , barn ) ;return 0 ;}



0 0