牛客網暑期ACM多校訓練營(第七場)C Bit Compression(暴力)

連接:https://www.nowcoder.com/acm/contest/145/C
來源:牛客網
 c++

Bit Compressionapp

時間限制:C/C++ 2秒,其餘語言4秒
空間限制:C/C++ 262144K,其餘語言524288K
Special Judge, 64bit IO Format: %lldspa

題目描述

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..net

輸入描述:

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.

示例1code

輸入

複製orm

2
1001

輸出

複製ci

4

說明

The sequences (XOR, OR), (XOR, AND), (OR, OR), (OR, AND) works.

題意:給你一個n,再給你一個長度爲2的n次方的01串。每次字符串能夠選擇三種操做:& | ^。選擇以後,相鄰的字符按這個操做合併:好比 1101 選擇^  則   1^1=0  0^1=1; 而後字符串就變成了01。繼續操做直到剩餘一個字符。問你最後只剩一個1的方法總數有多少種。字符串

思路:get

比賽的時候不敢暴力,再說過的人數那麼少。。。實際上就是純暴力,dfs的話題解雖說要剪枝,可是其實不減枝也能過。我看到一我的用map直接暴力也過了。。。input

因而代碼:

#include<bits/stdc++.h>
using namespace std;
map<string,int> mp[20];
map<string,int>::iterator it;
int main()
{
    int n;
    string s,sa,sb,sc;
    cin>>n>>s;
    mp[n][s]=1;
    for(int i=n;i>=1;i--)
    {
        for(it=mp[i].begin();it!=mp[i].end();it++)
        {
            s=it->first;
            int s1=it->second;
            int len=1<<i;
            sa=sb=sc="";
            for(int j=0;j<len;j+=2)
            {
                sa+=((s[j]-'0')&(s[j+1]-'0'))+'0';
                sb+=((s[j]-'0')^(s[j+1]-'0'))+'0';
                sc+=((s[j]-'0')|(s[j+1]-'0'))+'0';
            }
            mp[i-1][sa]+=s1;
            mp[i-1][sb]+=s1;
            mp[i-1][sc]+=s1;
        }
    }
    cout<<mp[0]["1"]<<endl;
    return 0;
}

附dfs(用的別人的代碼,侵刪):

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+5;
int tree[N*50];
char str[N];
int res;
void dfs(int n)
{
    if(n==-1)
    {
        if(tree[1]==1)
            res++;
        return ;
    }
    int k=1<<n;
    for(int j=0; j<=2; j++)
    {
        int cou=0;
        for(int i=0; i<k; i++)
        {
            int tmp=i+k;
            if(j==0)
                tree[tmp]=tree[tmp*2]^tree[tmp*2+1];
            else if(j==1)
                tree[tmp]=tree[tmp*2]|tree[tmp*2+1];
            else
                tree[tmp]=tree[tmp*2]&tree[tmp*2+1];

            if(tree[tmp]==0)
                cou++;
        }
        if(cou==k)
            continue;
        else
            dfs(n-1);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",str);
    int k=1<<n;
    for(int i=0; i<k; i++)
        tree[i+k]=str[i]-'0';
    dfs(n-1);
    printf("%d\n",res);
    return 0;
}

 

本文分享 CSDN - LSD20164388。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索