链接: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.
题意:给出一个01串,每次从&、|、^里面选一个,对这个01串进行相应的操作,问最后得到1有多少中方法。
方法:就是思维。记录得到某个串有几种方法,再由这个串出发,得到的方法累加。
不懂的话可以手动运行3 11001100这个样例,再结合我上面说的记录得到某个串几种方法,递推下去。
int main()
{
map<int,string>m;//第一个是关键字,first,键。第二个是键值。
m[0]="休闲无心";
m[1]="大好河山";
m[2]="木青子";
m[3]="红颜祸水";
map<int,string>::iterator Map_iter;
for(Map_iter=m.begin();Map_iter!=m.end();Map_iter++)
{
cout<<"key:"<<Map_iter->first<<" "<<"map:"<<Map_iter->second<<endl;
}
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#include<bits/stdc++.h>
using namespace std;
map<string,int> mp[20];//string是关键字it->first.
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;
}