# 51nod round3# 序列分解（折半枚举+字典树）

1.他们不能相互重叠。

2.他们要完全一样。

Input
第一行给出一个T，表示T组数据。(1<=T<=5)

Output
如果小刀可以完成游戏，输出"Good job!!" (不包含引号)，否则 输出"What a pity!" (不包含引号)。
Input示例
2
4
1 1 2 2
6
1 2 3 4 5 6
Output示例
Good job!!
What a pity!

//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;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：51nod round3# 序列分解（折半枚举+字典树） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)