CodeForces 282C(位运算)

C. XOR and OR
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.

A Bitlandish string is a string made only of characters "0" and "1".

BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish string a, and applies several (possibly zero) operations to it. In one operation the mayor may take any two adjacent characters of a string, define one of them as x and the other one as y. Then he calculates two values p and q: p = x xor y, q = x or y. Then he replaces one of the two taken characters by p and the other one by q.

The xor operation means the bitwise excluding OR operation. The or operation is the bitwise OR operation.

So for example one operation can transform string 11 to string 10 or to string 01. String 1 cannot be transformed into any other string.

You've got two Bitlandish strings a and b. Your task is to check if it is possible for BitHaval to transform string a to string b in several (possibly zero) described operations.

Input

The first line contains Bitlandish string a, the second line contains Bitlandish string b. The strings can have different lengths.

It is guaranteed that the given strings only consist of characters "0" and "1". The strings are not empty, their length doesn't exceed 106.

Output

Print "YES" if a can be transformed into b, otherwise print "NO". Please do not print the quotes.

Examples
Input
11
10
Output
YES
Input
1
01
Output
NO
Input
000
101
Output
NO

题意:给出两个字符串a, b,可以对a字符串中任意相邻的两个字符x,y进行位异或和按位或操作得到p=x^y;q=x|y;
再用p,q代替原来的x,y(可以交换顺序),问能否得到目标串b;
思路:
1:如果a和b的长度不同,则肯定不行;
2:由位运算法则我们可以知道有:
0^0=0, 0|0=0;
1^0=1, 1|0=1;
0^1=1, 0|1=1;
1^1=0, 1|1=1;
据此可以得知:(1,1)可以得到(0,1)或者(1,0);(0,1)或者(1,0)可以得到(1,1);(0,0)只能得到(0,0);
即有1的串不能完全消掉1(若a串长度为len1,且含有1,那么其可以得到一个含有x个1的串,x>=1&&x<=len1);没有1的串不能得到1;
所以只有当a,和b同时含有1或者不含1时可行;

代码:
 1 #include <bits/stdc++.h>
 2 #define MAXN 100000+10
 3 #define ll long long
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
 9     string a, b;
10     cin >> a >> b;
11     int len1=a.size();
12     int len2=b.size();
13     if(len1!=len2)
14     {
15         cout << "NO" << endl;
16         return 0;
17     }
18     if(len1==1&&a[0]=='1')
19     {
20         if(b[0]=='1') cout << "YES" << endl;
21         else cout << "NO" << endl;
22         return 0;
23     }
24     int ans1=0, ans2=0;
25     for(int i=0; i<len1; i++)
26     {
27         if(a[i]=='1')  ans1++;
28         if(b[i]=='1')  ans2++;
29     }
30     if(!ans1&&!ans2||ans1&&ans2)
31     cout << "YES" << endl;
32     else cout << "NO" << endl;
33     return 0;
34 }

 

 

艰难困苦,玉汝于成 —————— geloutingyu

转载于:https://www.cnblogs.com/geloutingyu/p/5877030.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces 1806C题目链接:https://codeforces.com/problemset/problem/1806/C 题目描述: 给定一个长度为 $n$ 的整数序列 $a_1,a_2,\dots,a_n$,你可以进行任意多次操作,每次操作可以选择一个区间 $[l,r]$,将区间内的所有数加上 $1$ 或者减去 $1$。你的目标是使得序列中的所有数都相等,求最小的操作次数。 解题思路: 首先考虑对于一个数来说,如何将它变成序列中的所有数。由于每次操作可以将一个区间内的数全部加上或减去 $1$,所以我们可以将该数变成序列中的中位数。中位数是序列中所有数排完序后处在中间的数,如果序列长度为偶数,则中位数是中间两个数的平均数。 接下来考虑对于整个序列来说,如何将所有数变成相等的数。我们可以先将所有数变成中位数,然后计算每个数与中位数的差值之和,这个值就是最小的操作次数。 最后考虑如何将一个数变成中位数。设序列长度为 $n$,$x$ 为中位数,则有以下两种情况: - 当 $n$ 为奇数时,$x=a_{\lfloor\frac{n+1}{2}\rfloor}$。 - 当 $n$ 为偶数时,$x=\frac{a_{\frac{n}{2}}+a_{\frac{n}{2}+1}}{2}$。 对于第一种情况,我们可以将序列中所有数先减去 $a_{\lfloor\frac{n+1}{2}\rfloor}$,然后再进行操作。 对于第二种情况,我们可以将序列中所有数先减去 $\frac{a_{\frac{n}{2}}+a_{\frac{n}{2}+1}}{2}$,然后再进行操作。注意,当 $n=2$ 时,$\frac{n}{2}=1$,需要特判。 代码实现:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值