ZOJ3432 Find the Lost Sock,异或运算
2011-09-01 16:24
423 查看
ZOJ上面的AC终于超过了100了
。
这道题有点难度,首先是输入的数据量大,最大的测试集达到1000000*2-1。其次一般人的想法是对于每一个输入,检索以前的输入是否有与之匹配的,如果有则消去,否则将输入记下来。
但是计算机里面有个非常有用的运算,就是异或了。而且异或符合交换律和结合律,假设异或记为xor,则有
根据以上的规律,我们可以对设置一个字符串数组,对于每一次输入,将输入的每一位和字符串数组的相应位进行异或运算,输入结束后的字符串数组就是缺失的袜子了。一下为代码。
。
这道题有点难度,首先是输入的数据量大,最大的测试集达到1000000*2-1。其次一般人的想法是对于每一个输入,检索以前的输入是否有与之匹配的,如果有则消去,否则将输入记下来。
但是计算机里面有个非常有用的运算,就是异或了。而且异或符合交换律和结合律,假设异或记为xor,则有
A xor B xor A = A xor ( B xor A ) = A xor A xor B = B
根据以上的规律,我们可以对设置一个字符串数组,对于每一次输入,将输入的每一位和字符串数组的相应位进行异或运算,输入结束后的字符串数组就是缺失的袜子了。一下为代码。
/******************************************************************************* * Author : Neo Fung * Email : neosfung@gmail.com * Last modified : 2011-09-01 16:17 * Filename : ZOJ3432 Find the Lost Sock.cpp * Description : * *****************************************************************************/ // ZOJ3432 Find the Lost Sock.cpp : Defines the entry point for the console application. // // #include "stdafx.h" #include <stdio.h> #include <string.h> #include <string> using namespace std; int main(void) { int n; char str[8],ch; while(scanf("%d",&n)!=EOF) { memset(str,'\0',sizeof(str)); for(int i=0;i<n*2-1;++i) { getchar(); for(int j=0;j<7;++j) { ch=getchar(); str[j] = str[j]^ch; } } printf("%s\n",str); } return 0; }
相关文章推荐
- ZOJ3432 Find the Lost Sock(异或运算)
- zoj3432 Find the Lost Sock 亦或的运用
- 异或解题:Find the Lost Sock
- ZOJ 3432 Find the Lost Sock (异或的运用)
- zoj 3432 Find the Lost Sock
- 389. Find the Difference注意异或运算的巧妙使用^
- ZOJ 3432 Find the Lost Sock
- zoj 3432 Find the Lost Sock(zoj月赛,受打击了 = =)
- ZOJ 3432 Find the Lost Sock
- zoj 3432 Find the Lost Sock
- ZOJ 3432 Find the Lost Sock 解题报告
- zoj3432——Find the Lost Sock
- ZOJ 3432 Find the Lost Sock
- ZCMU-1416-Find the Lost Sock
- zoj 3432 Find the Lost Sock(解决时间超时问题)
- ZOJ 3432 Find the Lost Sock(模拟)
- 1416: Find the Lost Sock
- LeetCode 389 Find the Difference (异或)
- LeetCode 389. Find the Difference(异或)
- JOJ2519 Find the longest section(位运算 +(前缀和?))