牛客多校第七场 C Bit Compression

链接:https://www.nowcoder.com/acm/contest/145/C
来源:牛客网
 

题目描述

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暴力剪枝,用unorder_map标记可以飘过。我的代码得看评测机心情。。。

还有就是想题解那样预处理出16位的情况。

#include<bits/stdc++.h>
#include<ext/rope>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<math.h>
#define maxn 500010
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
using namespace __gnu_cxx;
unordered_map<string,int> m;
void AND(string &s,int l)
{
    int i,j;
    for(i=0,j=0;i<l;i+=2,j++)
    {
        s[j]=((s[i]-'0')&(s[i+1]-'0'))+'0';
    }
    s.erase(s.begin()+l/2,s.end());;
}
void OR(string &s,int l)
{
    int i,j;
    for(i=0,j=0;i<l;i+=2,j++)
    {
        s[j]=((s[i]-'0')|(s[i+1]-'0'))+'0';
    }
    s.erase(s.begin()+l/2,s.end());
}
void XOR(string &s,int l)
{
    int i,j;
    for(i=0,j=0;i<l;i+=2,j++)
    {
        s[j]=((s[i]-'0')^(s[i+1]-'0'))+'0';
    }
    s.erase(s.begin()+l/2,s.end());
}
ll dfs(string s,int l)
{
    if(l==1)
    {
        if(s[0]=='1')
            return 1;
        return 0;
    }
    if(m[s])
        return m[s];
    for(int i=0;i<l;i++)
    {
        if(s[i]!='0')
        {
            ll ans=0;
            string d;
            d=s;
            AND(d,l);
            ans+=dfs(d,l>>1);
            d=s;
            OR(d,l);
            ans+=dfs(d,l>>1);
            d=s;
            XOR(d,l);
            ans+=dfs(d,l>>1);
            m[s]=ans;
            return ans;
        }
    }
    return 0;
}
int main()
{
    string s;
    int n;
    while(cin>>n)
    {
        cin>>s;
        cout<<(dfs(s,s.length()))<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值