数据并不大,dfs回溯即可。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 30; 7 char str[N]; 8 int digit[N]; 9 10 int dfs( int sum ) 11 { 12 if ( sum > 31 ) return 1; 13 for ( int i = 1; i <= 6; i++ ) 14 { 15 if ( digit[i] < 4 ) 16 { 17 digit[i]++; 18 int state = dfs( sum + i ); 19 digit[i]--; 20 if ( !state ) return 1; 21 } 22 } 23 return 0; 24 } 25 26 int main () 27 { 28 while ( scanf("%s", str) != EOF ) 29 { 30 memset( digit, 0, sizeof(digit) ); 31 int s = 0, len = strlen(str); 32 for ( int i = 0; i < len; i++ ) 33 { 34 int tmp = str[i] - ‘0‘; 35 s += tmp; 36 digit[tmp]++; 37 } 38 int dd = ( len & 1 ); 39 if ( dfs(s) ^ dd ) 40 { 41 printf("%s A\n", str); 42 } 43 else 44 { 45 printf("%s B\n", str); 46 } 47 } 48 return 0; 49 }
时间: 2024-11-03 22:50:06