Topic Link: Portal
Test instructions
Divides a sequence of length n into identical sub-sequences and cannot have a cross.
Analysis:
For a number we first put it in the va[] array, if the next number to be discussed is the same as the first number in VA
Then this number is probably the first number of the second sequence.
Then there are several situations:
1) This number is a number in the middle of the V1, directly into the V1.
2) The first number of the V1 is placed in the V2,
code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < Cstring> #include <queue>using namespace std;const int maxn = 50;bool flag;int a[maxn];int n;int Q1[MAXN],Q2[MAXN ];void dfs (int id,int len1,int Len2,int st) {if (len1>n/2| | len2>n/2| | Flag) return; if (id==n) {if (len1==len2) flag=1; Return } if (id==0) {Q1[len1]=a[id]; DFS (ID+1,LEN1+1,LEN2,ST); } else{if (A[id]==q1[st]) {Q2[len2]=a[id]; DFS (ID+1,LEN1,LEN2+1,ST+1); } Q1[len1]=a[id]; DFS (ID+1,LEN1+1,LEN2,ST); }}int Main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=0;i<n;i++) {scanf ("%d", a+i); } memset (Q1,0,sizeof (Q1)); memset (q2,0,sizeof (q2)); flag=0; DFS (0,0,0,0); if (flag) puts ("Good job!!"); Else puts ("What a pity!"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
51nod 1400 sequence decomposition (DFS + pruning)