poj 4095(位操作)

位操作应用——异或

http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=4095

Sample Input

2
aabcdef
bzyxwvu
bzyxwvu
4
aqwerty
eas fgh
aqwerty
easdfgh
easdfgh
aqwerty
aqwerty
2
0x0abcd
0ABCDEF
0x0abcd

Sample Output

aabcdef
eas fgh
0ABCDEF

题意:购买了n对袜子,每对袜子都有自己的名字(即7个字符)。丢失了一只,找出丢失的袜子的名字。

首先想到的是对二维字符数组排序,这样很费空间。

还有一种做法就是用异或操作。

用两个字符串A[]和b[]分别记录前一次的结果和新输入的字符串,对同下标的字符进行异或操作,最后结果就是A。

异或运算满足交换律:B^C^C^A^B = A^B^B^C^C = A; 所以就不用担心顺序问题了。

异或操作介绍详细参看:http://baike.baidu.com/view/674171.htm

原文地址:https://www.cnblogs.com/submarinex/p/1941233.html