Oleg and Little Ponies

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+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}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=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,k,t;
bool flag;
set<int>ok;
vi tmp;
char b[maxn];
bitset<maxn>a[maxn<<2],c[maxn<<2],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,1,m)
    {
        scanf("%s",b);
        ok.insert(i);
        for(j=0;b[j];j++)if(b[j]==‘1‘)a[i].set(j);
        scanf("%s",b);
        for(j=0;b[j];j++)if(b[j]==‘1‘)c[i].set(j);
    }
    scanf("%s",b);
    for(j=0;b[j];j++)if(b[j]==‘1‘)ans.set(j);
    rep(i,1,m)gao(i);
    for(int x:tmp)ok.erase(x);
    while(1)
    {
        flag=false;
        for(int x:ok)
        {
            gao(x);
        }
        if(!flag)break;
        for(int x:tmp)ok.erase(x);
        tmp.clear();
    }
    for(i=0;i<n;i++)cout<<ans[i];
    printf("\n");
    return 0;
}
时间: 2024-08-05 18:14:54

Oleg and Little Ponies的相关文章

Oleg Sych - ? Pros and Cons of T4 in Visual Studio 2008

Oleg Sych - ? Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Posted by Oleg Sych January 1, 2009 T4 is a fully-featured, template-based code generation engine built into Visual Studio 2008. It offers rich function

@codeforces - [email&#160;protected] Oleg and chess

目录 @description - [email protected] @[email protected] @part - [email protected] @part - [email protected] @part - [email protected] @part - [email protected] @accepted [email protected] @[email protected] @description - [email protected] 给定一个 n*n 的棋

CF793G Oleg and chess [线段树优化建边,扫描线,最大流]

不相交就直接搞啊..没啥技巧,i->j如果选了就是(i,j)选了. // powered by c++11 // by Isaunoya #include <bits/stdc++.h> #define rep(i, x, y) for (register int i = (x); i <= (y); ++i) #define Rep(i, x, y) for (register int i = (x); i >= (y); --i) using namespace std;

ural 2017 Best of a bad lot

2017. Best of a bad lot Time limit: 1.0 secondMemory limit: 64 MB A cruise liner hasn’t moved away from the land even for three miles when it became apparent that somebody has drowned one of the stewards in the swimming pool. The captain promised to

A Complete ActiveX Web Control Tutorial

? Introduction ActiveX is a Microsoft technology developed in the mid 90's, that allows for the creation of applet-like applications that can be downloaded and run within Microsoft's Web browser. This article is intended for Visual C++ developers who

Raft算法国际论文全翻译

最近在开发强一致性的分布式算法,因此需要深入理解下Raft算法,这里对Raft论文进行了翻译,留以备用 - Sunface. 英文版论文:https://ramcloud.atlassian.net/wiki/download/attachments/6586375/raft.pdf Raft 是一种通过日志复制来实现的一致性算法,提供了和(多重)Paxos 算法相同的功能和性能,但是它的算法结构和 Paxos 是不同的,因此Raft 算法更容易理解和应用.Raft 有几个关键模块:领导人选举.

手册的选择器部分

1,#id   $("#div"); 查找html标签中id为div的元素 <div id="div"></div> 2,element $("div");   查找标签中 元素为div的所有元素 <div>1</div> <div>1</div> <p>1</p> 3,.class $(".a"); 查找标签中class为a的所有

垃圾邮件过滤优化方法

垃圾邮件过滤优化方法 通过honeypot project 搜集大量垃圾邮件数据 通过解析邮件header 获取垃圾邮件发送路径和服务器相关信息 对编写错误的单词的修正 比如:w4tch 对相同含义的词进行归类处理,比如:discount 和discounts   (可以通过porter stemmer,下面就是该算法c语言的一种实现) /* This is the Porter stemming algorithm, coded up in ANSI C by the author. It m

URAL 1083. Factorials!!! (阅读理解)

1083. Factorials!!! Time limit: 1.0 second Memory limit: 64 MB Definition 1. n!!-! = n(n?k)(n?2k)-(n mod k), if k doesn't divide n; n!!-! = n(n?k)(n?2k)-k, if k divides n (There are k marks ! in the both cases). Definition 2.X mod Y - a remainder aft