nefu 643 按位异或应用

http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=643

description

This time,suddenly,teacher Li wants to find out who have missed interesting DP lesson to have fun.The students who are found out will get strictly punishment.Because,teacher Li wants all the students master the DP algorithm.
    However,Li doesn't want to waste the class time to call over the names of students.So he let the students to write down their names in one paper.To his satisfaction,this time, only one student has not come.
    He can get the name who has not come to class,but,it is troublesome,and,teacher always have many things to think about,so,teacher Li wants you, who is in the ACM team, to pick out the name.
							

input

There are several test cases.The first line of each case have one positive integer N.N is the  number of the students,and N will not greater than 500,000.
   Then,following N lines,each line contains one name of students who have attended class.The N-1 lines are presented after N lines.These N-1 lines indicates the names of students who have come to class this time,one name in one line.
   The length of student's name is not greater than 30.
   Process to the end of file.

output

 For each test case, first print a line saying "Scenario #k", where k is the number of the test case.Then output the name of the student who have not come to class.One case per line.Print a blank line after each test case, even after the last one.

sample_input

3
A
B
C
B
C

sample_output

Scenario #1
A
这道题比赛的时候我以为用map可以,可是数据太厉害了,怎么弄都超时。又改成了异或,这样就对了。

按位异或:

 
孙常峰学长 17:15:56 
1^1=0
1^0=1
孙常峰学长 17:16:33 
然后拓展开
孙常峰学长 17:16:42 
101 ^ 101 ^ 111 = 111
孙常峰学长 17:16:47 
即 A^A^B=B
孙常峰学长 17:16:52 
A^A^A=A
孙常峰学长 17:17:14 
有偶数个A异或就会相互抵消
Yran 17:17:33 
字符串也行嘛
Yran 17:17:35 
??、
孙常峰学长 17:17:51 
再举个例子
Yran 17:18:03 
恩
孙常峰学长 17:18:05 
ABC
ABC
BCD
按位异或
孙常峰学长 17:18:20 
A^A^B = B
B^B^C = C
C^C^D = D
孙常峰学长 17:18:29 
发现关键了没有
Yran 17:18:49 
你是说一位一位的来的
孙常峰学长 17:18:52 
不理解的话我们把一个字符'A'换成asc2码
孙常峰学长 17:18:58 
'A' = 65
孙常峰学长 17:19:01 
'B' = 66
孙常峰学长 17:19:29 
65 1000001
孙常峰学长 17:19:38 
66 1000010
孙常峰学长 17:20:03 
'A' ^ 'A' ^ 'B'
1000001 ^ 1000001 ^ 1000010
孙常峰学长 17:20:09 
你说是多少
孙常峰学长 17:20:11 
结果
Yran 17:20:30 
1000010
孙常峰学长 17:20:40 
 是不是'B'
孙常峰学长 17:20:42 
这就说明
Yran 17:20:46 
我去
Yran 17:20:53 
你接着说
孙常峰学长 17:21:01 
对字符按位异或
孙常峰学长 17:21:07 
还能保持异或的性质
孙常峰学长 17:21:21 
即 偶数个 相同的抵消
孙常峰学长 17:21:41 
我们再拓展,把字符拓展成字符串
孙常峰学长 17:21:57 
"ABC" 
"ABC"
"BCD"
孙常峰学长 17:22:06 
对第一位的字符
孙常峰学长 17:22:12 
"A" "A" "B"
孙常峰学长 17:22:18 
异或起来
孙常峰学长 17:22:26 
再对第二位 ,第三位异或
孙常峰学长 17:22:28 
结果就是
孙常峰学长 17:22:33 
"BCD"
孙常峰学长 17:22:36 
没了
Yran 17:25:49 
有点明白了,
孙常峰学长 17:25:57 
才有点啊
孙常峰学长 17:26:02 
我再说几个性质
孙常峰学长 17:26:17 
0异或任何数都是任何数
孙常峰学长 17:26:27 
0 ^ A = A
Yran 17:26:30 
恩恩
Yran 17:26:34 
继续说
孙常峰学长 17:26:44 
异或满足结合律
孙常峰学长 17:26:52 
A^B^C = A^(B^C)
孙常峰学长 17:27:08 
交换律
孙常峰学长 17:27:13 
A^B^A = A^A^B
孙常峰学长 17:27:24 
没了
孙常峰学长 17:27:34 
总之题意就是
孙常峰学长 17:27:42 
A^B^C^D^A^C^D
孙常峰学长 17:27:56 
=A^A^C^C^D^D^B
孙常峰学长 17:27:58 
=B



 我的代码: 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdio.h>
int main()
{
    char a[35],b[35];
    int n;
    int k=1;
    while(~scanf("%d",&n))
    {
        if(n<=0)
            break;
        memset(b,0,sizeof(b));
        memset(a,0,sizeof(a));
        for(int i=0;i<2*n-1;i++)
        {
            scanf("%s",a);
            int m=strlen(a);
            int kk=strlen(b);
            for(int j=0;j<m||j<kk;j++)
                b[j]=b[j]^a[j];
        }
        printf("Scenario #%d\n%s\n\n",k++,b);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值