链接:https://www.nowcoder.com/acm/contest/145/C
来源:牛客网
Bit Compression
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
A binary string s of length N = 2n is given. You will perform the following operation n times :
- Choose one of the operators AND (&), OR (|) or XOR (^). Suppose the current string is S = s1s2…sk. Then, for all , replace s2i-1s2i with the result obtained by applying the operator to s2i-1 and s2i. For example, if we apply XOR to {1101} we get {01}.
After n operations, the string will have length 1.
There are 3n ways to choose the n operations in total. How many of these ways will give 1 as the only character of the final string.
输入描述:
The first line of input contains a single integer n (1 ≤ n ≤ 18).
The next line of input contains a single binary string s (|s| = 2n). All characters of s are either 0 or 1.
输出描述:
Output a single integer, the answer to the problem.
示例1
输入
复制
2
1001
输出
复制
4
说明
The sequences (XOR, OR), (XOR, AND), (OR, OR), (OR, AND) works.
思路:就是dfs纯暴力,当时过的人太少,以为会很难
加了个剪枝,当数组中的值都为0时,return;
当数组中的值为两个时,就进行判断是否最后能变成1,然后return;
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 300005;
char s[N];
int cnt = 0;
void dfs(char tmp[],int len)
{
int sum = 0;
for(int i = 0;i < len;++i){
if(tmp[i] == '0'){
sum++;
}
}
if(sum == len)
return ;
if(len == 2){
if(tmp[0] == '1' || tmp[1] == '1'){
cnt += 2;
}
return ;
}
char ans[N];
for(int i = 1;i < len;i += 2)
{
ans[i / 2] = ((tmp[i] - '0') & (tmp[i - 1] - '0')) + '0';
}
dfs(ans,len / 2);
for(int i = 1;i < len;i += 2)
{
ans[i / 2] = ((tmp[i] - '0') | (tmp[i - 1] - '0')) + '0';
}
dfs(ans,len / 2);
for(int i = 1;i < len;i += 2)
{
ans[i / 2] = ((tmp[i] - '0') ^ (tmp[i - 1] - '0')) + '0';
}
dfs(ans,len / 2);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
scanf("%s",s);
int len = strlen(s);
cnt = 0;
dfs(s,len);
printf("%d\n",cnt);
}
return 0;
}