Question: buct1766 pku1270
Method: DFS
Idea: use search or next_permutation to generate the next arrangement, and then determine whether the arrangement meets
Required. output only if required.
Code:
# Include <iostream> # include <algorithm> # include <cstring> using namespace STD; char var [50], t [50]; char constraint [105] [2]; bool visited [50]; int n, m, POS [305]; void DFS (int K) {int I, j; If (k = N) {// position of the updated character for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) if (constraint [I] [0] = T [J]) {pos [constraint [I] [0] = J; break;} For (j = 0; j <n; j ++) if (constraint [I] [1] = T [J]) {pos [constraint [I] [1] = J; break;} // check whether there are for (I = 0; I <m; I ++) {for (j = POS [constraint [I] [0]-1; j> = 0; j --) if (T [J] = constraint [I] [1]) break; If (j> = 0) break;} if (I <m) return; // if there is any violation, you do not need to output for (I = 0; I <n; I ++) if (I <n-1) cout <t [I]; else cout <t [I] <Endl; return ;}for (I = 0; I <n; I ++) if (visited [I] = 0) {char TMP = var [k]; POS [k] = I; t [k] = var [I]; visited [I] = 1; DFS (k + 1 ); T [k] = TMP; visited [I] = 0; POS [k] = K;} int main () {int flag = 0; char C; n = 0, m = 0; while (scanf ("% C", & C )! = EOF) {If (C> = 'A' & C <= 'Z') var [n ++] = C; while (C = getchar ())! = '\ N') if (C! = '') {Var [N] = C; POS [c] = N; n ++;} while (C = getchar ())! = '\ N') if (C! = '') {If (M % 2 = 0) Constraint [m/2] [0] = C; else constraint [m/2] [1] = C; M ++;} m/= 2; int I, j; If (FLAG) cout <Endl; sort (VAR, VAR + n); // must be sorted, otherwise, wrong answer DFS (0); n = 0; M = 0; flag = 1;} return 0 ;}
Code 2: Use next_permutation
# Include <iostream> # include <algorithm> using namespace STD; int n, m; // n variables. The M variables must be char var [50]. char constraint [105] [2]; int POS [150]; int main (INT argc, char * argv []) {int flag = 0; char C; n = 0; m = 0; while (scanf ("% C", & C )! = EOF) {If (C> = 'A' & C <= 'Z') var [n ++] = C; while (C = getchar ())! = '\ N') if (C! = '') {Var [N] = C; POS [c] = N; n ++;} while (C = getchar ())! = '\ N') if (C! = '') {If (M % 2 = 0) Constraint [m/2] [0] = C; else constraint [m/2] [1] = C; M ++;} m/= 2; int I, j; If (FLAG) cout <Endl; sort (VAR, VAR + n ); // note that the position corresponding to the characters must be updated during the arrangement change./* for (I = 0; I <m; I ++) cout <constraint [I] [0] <''<constraint [I] [1] <Endl <POS [constraint [I] [0] <''<POS [constraint [I] [1] <Endl; */do {// modify the position of the character corresponding to the changed arrangement for (I = 0; I <m; I ++) {for (j = 0; j <N; j ++) if (constraint [I] [0] = var [J]) {pos [constraint [I] [0] = J; break ;} for (j = 0; j <n; j ++) if (constraint [I] [1] = var [J]) {pos [constraint [I] [1] = J; break ;}/// check whether there are for (I = 0; I <m; I ++) {for (j = POS [constraint [I] [0]-1; j> = 0; j --) if (VAR [J] = constraint [I] [1]) break; If (j> = 0) break;} if (I <m) continue; for (I = 0; I <n; I ++) if (I <n-1) cout <var [I]; else cout <var [I] <Endl ;}while (next_permutation (VAR, VAR + n); n = 0; M = 0; flag = 1 ;} return 0 ;}
Both codes can only be AC on the buct and re on the pku. I don't know why, so further consideration is needed.