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.
Sample test(s)input
1110
output
YES
input
101
output
NO
input
000101
output
NO
解題說明:此題就是判斷一個只含01字串能否由另一個字串通過相鄰位置元素XOR或OR操作得到。首先可以肯定的是這兩個字串必須相同,還有就是必須在有1存在的情況下才能通過XOR或OR操作得到1,因為0 XOR 0=0,0 OR 0=0。同時如果字串中有1,通過OR操作是無法消去1的。綜上可知,要麼這兩個字串中都有1,要麼都沒有1, 至於位置沒有什麼規律,比較任意。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;char a[1000001],b[1000001];int main(){int i,m,n;int num1,num2;scanf("%s",&a);scanf("%s",&b);m=strlen(a);n=strlen(b);num1=num2=0;if(m!=n){printf("NO\n");}else{for(i=0;i<m;i++){if(a[i]=='1'){num1++;}}for(i=0;i<n;i++){if(b[i]=='1'){num2++;}}if((num1==0&&num2==0)||(num1!=0&&num2!=0)){printf("YES\n");}else{printf("NO\n");}}return 0;}