2 seconds
256 megabytes
standard input
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.
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.
Print "YES" if a can be transformed into b, otherwise print "NO". Please do not print the quotes.
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时可行; 代码:
#include <bits/stdc++.h>
#define MAXN 100000+10
#define ll long long
using namespace std; int main(void)
{
std::ios::sync_with_stdio(false), cin.tie(), cout.tie();
string a, b;
cin >> a >> b;
int len1=a.size();
int len2=b.size();
if(len1!=len2)
{
cout << "NO" << endl;
return ;
}
if(len1==&&a[]=='')
{
if(b[]=='') cout << "YES" << endl;
else cout << "NO" << endl;
return ;
}
int ans1=, ans2=;
for(int i=; i<len1; i++)
{
if(a[i]=='') ans1++;
if(b[i]=='') ans2++;
}
if(!ans1&&!ans2||ans1&&ans2)
cout << "YES" << endl;
else cout << "NO" << endl;
return ;
}
艰难困苦,玉汝于成 —————— geloutingyu