Oleg and Little Ponies

Time limit: 0.9 second
Memory limit: 64 MB
Little boy Oleg loves the cartoon My Little Pony. How it cannot be loved, because there is friendship, magic and colored horses!
For the past several months Oleg has been begging from his parents a real pony, but they are just ready to buy him only collectible figures of cartoon characters. With these figures Oleg recreates the best episodes of My Little Pony on his desk. Sometimes he realizes that he has already all the key characters for the next episode, and begins to feel the desire to immediately buy the missing figures for this episode. For example, if Oleg has on hand Twilight Sparkle and Spike, his life will not be sweet without Princess Celestia. It may happen that the new figures will cause new desires: having three above-mentioned figures, Oleg will want Nightmare Moon.
For convenience, let’s number all the figures with integers from 1 to n. Then the Oleg’s desirewill be described by two sets of numbers {a1, ..., ak} and {b1, ..., bt}, which means that if he already has figures with numbers a1, ..., ak, he also wants figures with numbers b1, ..., bt.
Oleg’s parents in order to distract him from his desires of real pony are ready to buy him as many figures as he wants. But they want to buy a set of figures that will satisfy all the desires of Oleg, in a single purchase. Of course, parents will not buy the extra figures.
What figures will Oleg have after purchase?

Input

The first line contains integers n and m that are the number of figures and the number of Oleg’s desires (1 ≤ n ≤ 1000; 0 ≤ m ≤ 4000). The following m lines describe the desires. Each desire is given by two sets, separated by a space. A set is a string of n characters, each of that is “0” or “1”. The figure with number i is in the set, only when i-th character of the string is “1”. The last line contains the set of figures, which Oleg already has, in the same format.

Output

In a single line output a set of figures, which Oleg will have after purchase. In other words, it is the union of the set of the existing figures and the set of figures bought by parents. Output format is the same as in the input.

Sample

input output
6 4
111000 101000
110000 111000
010000 100000
000010 000001
010100
111100

Notes

In the example Oleg has already the figures 2 and 4. First, he wants the figure 1 (the third desire). If he gets it, he will want the figure 3 (the second desire). If you just buy him the figures 1 and 3, Oleg will not want anything more (the right part of the first desire consists of already existing figures, and the left side of the last desire is not fulfilled). Thus, after purchase Oleg will have figures with the numbers 1, 2, 3 and 4.
分析:bitset基本用法;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
inline void umax(int &p,int q){if(p<q)p=q;}
inline void umin(int &p,int q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t;
bool flag;
set<int>ok;
vi tmp;
char b[maxn];
bitset<maxn>a[maxn<<],c[maxn<<],ans;
inline void gao(int p)
{
if((ans&a[p])!=a[p])return;
ans|=c[p];
tmp.pb(p);
flag=true;
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,m)
{
scanf("%s",b);
ok.insert(i);
for(j=;b[j];j++)if(b[j]=='')a[i].set(j);
scanf("%s",b);
for(j=;b[j];j++)if(b[j]=='')c[i].set(j);
}
scanf("%s",b);
for(j=;b[j];j++)if(b[j]=='')ans.set(j);
rep(i,,m)gao(i);
for(int x:tmp)ok.erase(x);
while()
{
flag=false;
for(int x:ok)
{
gao(x);
}
if(!flag)break;
for(int x:tmp)ok.erase(x);
tmp.clear();
}
for(i=;i<n;i++)cout<<ans[i];
printf("\n");
return ;
}

最新文章

  1. lua相关笔记
  2. csdn的app打开贴子显示空白?
  3. 自定义easyui整数或者数字、字母或者下划线验证方法
  4. 转: VMware 安装mac osx 10.11 安装步骤(一)(from伟东)
  5. Swift基础语法 、 元组(Tuple)
  6. 【转】高性能Javascript--脚本的无阻塞加载策略
  7. hdu 4255 A Famous Grid
  8. java获取远程网络图片文件流、压缩保存到本地
  9. 借用Toad 生成表空间的使用量图示
  10. 《精通CSS:高级Web标准解决方案》学习笔记(下)
  11. c - static 变量
  12. 使用第三方框架 Masonry 实现自动布局
  13. hdu 4736 This Is The Job The Bear Finds(2013年成都ACM网络赛)
  14. python - hash类型操作
  15. python-异常处理、元类
  16. sql server 查看锁表SQL【转】
  17. 从 Hadoop 1.0 到 Hadoop 2.0 ,你需要了解这些
  18. X的平方根(二分)
  19. vue 音乐播放器报错
  20. CocosPods 每次install pod 都卡在analyzing

热门文章

  1. 2017 Multi-University Training Contest - Team 2 &amp;&amp;hdu 6053 TrickGCD
  2. hdoj- Windows Message Queue
  3. E20170828-mk
  4. python 46 盒模型 与盒模型布局
  5. Android PopWindow的替代品BasePopup
  6. AE错误代码解释
  7. 5.12redis
  8. Laravel5.1学习笔记4 控制器
  9. Leetcode0019--Remove Nth Node From End of List 移除链表第N个结点
  10. javascript 基础知识点