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
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
Print "YES" if a can be transformed into b, otherwise print "NO". Please do not print the quotes.blog
11
10
YES
1
01
NO
000
101
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