51nod 1400 sequence decomposition (DFS + pruning)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.