dfs 51nod-1400-序列分解1400 序列分解

基准时间限制:1 秒 空间限制:131072 KB 分值: 40  难度:4级算法题
 收藏
 关注

小刀和大刀是双胞胎兄弟。今天他们玩一个有意思的游戏。 大刀给小刀准备了一个长度为n的整数序列。小刀试着把这个序列分解成两个长度为n/2的子序列。

这两个子序列必须满足以下两个条件:

1.他们不能相互重叠。

2.他们要完全一样。

如果小刀可以分解成功,大刀会给小刀一些糖果。

然而这个问题对于小刀来说太难了。他想请你来帮忙。


Input
第一行给出一个T,表示T组数据。(1<=T<=5)
接下来每一组数据,输入共2行。
第一行包含一个整数n (2<=n<=40且为偶数)。
第二行给出n个整数a[0],a[1],a[2],…,a[n-1]表示大刀给小刀准备的序列。(-1,000,000,000<=a[i]<=1,000,000,000)
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!
System Message  (题目提供者)


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
ll a[50],b1[50],b2[50];
int leap=0;
void dfs(int id,int len1,int len2,int len){
if(len1>n/2||len2>n/2||leap){
return ;
}
if(id==n)
if(len1==n/2&&len2==n/2){
leap=1;
return ;
}

if(a[id]==b1[len]){
b2[len2]=a[id];
dfs(id+1,len1,len2+1,len+1);
}
b1[len1]=a[id];
//printf("%d ",len1);
dfs(id+1,len1+1,len2,len);


}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
}
b1[0]=a[0];
leap=0;
dfs(1,1,0,0);

if(leap){
printf("Good job!!\n");
}
else{
printf("What a pity!\n");
}
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/80442371
今日推荐