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

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

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

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

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

Input

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

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

Output

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

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
相關文章
相關標籤/搜索