1. The number of division 2 stars
https://ac.nowcoder.com/acm/problem/16695
. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 #define LL Long Long . 4 #define EPS-1E. 6 . 5 int ANS = 0 ; . 6 int n-, K; . 7 void DFS ( int ID, int SUM, int Val) . 8 { . 9 / * IF (K-ID. 1 && == + SUM Val <= n-) {// the presence of the last digit n-sum> = val set which is incremented number 10 ANS ++; . 11 return; 12 is } 13 is for (int I = Val; I <= n-; I ++) { 14 if(sum + i >n) 15 break; 16 dfs( id + 1, sum + i, i); 17 }*/ 18 if(id==k&&sum==n) 19 { 20 ans++; 21 return; 22 } 23 24 if(id>=k||sum>n) 25 return; 26 27 for(int i = val; i <= n; i++){ 28 if(sum+i>n) 29 break; 30 dfs(id+1,sum+i,i); 31 } 32 } 33 34 int main() 35 { 36 while(scanf("%d %d", &n, &k) != EOF){ 37 ans = 0; 38 dfs(0, 0, 1); 39 printf("%d\n", ans); 40 } 41 return 0; 42 }
2. The word Solitaire 3 stars
. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 . 4 struct Node { . 5 char S [ 22 is ]; . 6 int len; . 7 int V; // number of times selected . 8 } C [ 22 is ]; . 9 int n, MAXN; 10 . 11 void DFS ( int X, int len) { // current word length Long current number 12 is for ( int I = . 1 ; I <= n; I ++) { // can be selected from the group of n word 13 is IF (C [I] .v < 2 ) // number of times smaller than the selected 2 14 { 15 for ( int J = 0 ; J <C [X] .LEN; J ++) { // a word is determined before and currently selected word have equal areas 16 IF (C [X] .s [J] == C [I] .s [ 0 ]) { . 17 int K = . 1 , T = . 1 , L; 18 is for (L J + = . 1 ; L <C [X] && .LEN K <C [I] .LEN; K ++, ++ L) { . 19 IF ! (C [X] .s [L] = C [I] .s [ K]) 20 is { 21 is T = 0; BREAK ; 22 is } 23 is } 24 IF (L = C [x] .LEN!) // DESCRIPTION word x i is a word comprising 25 T = 0 ; 26 is IF (T) // Description word x i is connected to the word and not contained 27 { 28 C [I] .v ++ ; 29 DFS (I, C + len [I] .len- K); 30 C [I] .v-- ; 31 is } 32 } 33 is } 34 is } 35 } 36 MAXN = max (MAXN, len); 37 [ } 38 is 39 int main () { 40 CIN n->>; // number of words 41 is for ( int I = . 1 ; I <= n-; I ++ ) { 42 is CIN C >> [I] .s; // first word content 43 is C [I] .LEN = strlen (C [I] .s); // first word length 44 is } 45 CIN >> C [ 0 ] .s; // idiom initials 46 is C [ 0 ] = .LEN strlen (C [ 0 ] .s); 47 48 dfs(0,c[0].len); 49 cout<<maxn<<endl; 50 }
Using dfs of: searching for a maximum value of the program program composed by several x, x there are n kinds of programs, for x in dfs void dfs ( int x) { for ( int I = 0 ; I <n; I ++ ) .. . }