The knife and Broadsword are twin brothers. Today they play an interesting game. The broadsword gives the knife a sequence of integers of length n. The knife tries to break down the sequence into two sub-sequences with a length of N/2.
These two sub-sequences must meet the following two conditions:
1. They cannot overlap each other.
2. They want exactly the same.
If the knife can be broken down successfully, the broadsword will give the knife some sweets.
However, this problem is too difficult for a knife. He wants to ask you to help.
Input
The first line gives a T, which represents the T-group data. (1<=t<=5) The next set of data, enter a total of 2 rows. The first line contains an integer n (2<=n<=40 and even). The second line gives n integers a[0],a[1],a[2],..., a[n-1] represents the series of knives prepared by a machete. ( -1,000,000,000<=a[i]<=1,000,000,000)
Output
If the knife can complete the game, output "good job!!" (not including quotation marks), otherwise output "what a pity!" (not including quotation marks).
Input example
241 1 2 261 2 3 4 5 6
Output example
Good job!! What a pity!
Idea: Divide the original string into two halves. Then the first half enumerates which give the first sequence, the remainder to the second sequence, the previous match, and if there is a string that is the prefix of another string, insert the remaining suffix into the dictionary tree. Otherwise, do not insert.
Then enumerate the latter half of the paragraph. This time to have a string is a suffix of another string, and then take the remaining prefix to the dictionary tree query. If there is, the description succeeds. And we need to disperse it first.
complexity is
Accepted 375 ms 96120 kb#include <iostream> #include <cstring> #include <cmath> #include <queue > #include <stack> #include <list> #include <map> #include <set> #include <sstream># Include <string> #include <vector> #include <cstdio> #include <ctime> #include <bitset># Include <algorithm> #define SZ (x) ((int) (x). Size ()) #define ALL (v) (v). Begin (), (v). End () #define foreach (I, v) for ( __typeof ((v). Begin ()) I = (v). Begin (); I! = (v). End (); + + i) #define Refeach (i, v) for (__typeof ((v). Rbegin ()) i = (v). Rbegin (); I! = (v). rend (); + + i) #define REP (i,n) for (int I=1; I<=int (n); i++) using namespace std;typedef long long ll;const int N = 1<<18;struct node{node* child[45]; BOOL Flag; Node () {memset (child,0,sizeof (child)); Flag = 0; }}trie[n];int sz;node *root,*cur;node* CreateNode () {memset (trie[sz].child,0,sizeof (trie[sz].child)); Trie[sz].flag = 0; Return &trie[sz++];} void Insert (int s[],int l,int R) {cur = root; for (int p = L; p <= R; p++) {int val = s[p]; if (cur->child[val] = = NULL) Cur->child[val] = CreateNode (); Cur = cur->child[val]; } Cur->flag = 1;} BOOL Query (int s[],int l,int R) {cur = root; for (int p = r;p >= L; p--) {int val = s[p]; if (cur->child[val] = = NULL) return false; Cur = cur->child[val]; } return Cur->flag;} Pair<int,int>b[50];int a[50];int s1[50],s2[50];int top1,top2;int main () {int T; cin>>t; while (t--) {sz = 0; root = CreateNode (); int n; scanf ("%d", &n); REP (i,n) scanf ("%d", &b[i].first), b[i].second = i; Sort (b+1,b+1+n); int maxx = 0; A[b[1].second] = Maxx; for (int i = 2; i<= n; i++) if (B[i].first = = B[i-1].first) A[b[i].second] = Maxx; else A[b[i].second] = ++maxx; for (int mask = 0; MasK < (1<<N/2); mask++) {top1 = TOP2 = 0; int i; for (i = 0; i < N/2; i++) {if (mask& (1<<i)) {s1[+ +TOP1] = a[i+1]; if (top1 <= top2 && s1[top1]! = S2[top1]) break; } else {S2[++TOP2] = a[i+1]; if (top2 <= top1 && s1[top2]! = S2[top2]) break; }} if (i = = N/2) {if (Top1 <= top2) Insert (S2,TOP1+1,TOP2); else Insert (S1,TOP2+1,TOP1); }} bool KO = 0; for (int mask = 0; mask < (1<<N/2) && KO = = 0; mask++) {top1 = TOP2 = 0; int i; for (i = 0; i < N/2; i++) {if (mask& (1<<i)) {s1[+ +TOP1] = a[n-i];if (top1 <= top2 && s1[top1]! = S2[top1]) break; } else {S2[++TOP2] = a[n-i]; if (top2 <= top1 && s1[top2]! = S2[top2]) break; }} if (i = = N/2) {if (top1 <= top2 && query (S2,TOP1+1,TOP2)) KO = 1; else if (Top1 > Top2 && Query (S1,TOP2+1,TOP1)) KO = 1; }} if (KO) 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 round3# sequence Decomposition (binary enumeration + dictionary tree)