【城会玩】hrbust 1341Who will be punished【思维】

来源:互联网 发布:2k18艾佛森捏脸数据 编辑:程序博客网 时间:2024/06/05 17:10

Who will be punishedTime Limit: 1000 MSMemory Limit: 32767 KTotal Submit: 543(91 users)Total Accepted: 204(78 users)Rating: Special Judge: NoDescription

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.

OutputFor 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


题目大意:有n个人名,n-1行到场的人名,输出没到场的人名。


思路:

1、暴力,每一次输入一个到场人名,都去人名册中匹配一次,对上了的人,vis【】数组对应标记一下,找到没有vis【】标记的那个人,复杂度O(n^2),明显超时。


2、使用STLmap映射,对应到场人名都映射一个int值,即mp【a【i】】=x;然后每输入一个到场的人名,对应vis【mp【a【i】】】标记一下,找到没有vis【】标记的那个人,复杂度O(n),不会超时,写了一发,提交,MLE,500000*30的char数组,超出32m内存,明显这个题的目的在于让你继续优化,使得无论是空间上还是时间上都优化到最极致。


3、思考无果,参考前辈们的思路:

①首先引入亦或位运算:二进制数中,相同为0,不同为1。

②我们不妨做一下这个亦或运算:2^3^4^3^4值为2,同理,我们在做一下这个亦或运算:4^5^8^10^5^4^10值为8.不难发现,我们可以将这个规律递推到字符串上。

③那么其亦或值,就是我们要得到的结果。


AC代码:


#include<stdio.h>#include<string.h>using namespace std;char a[50];char tmp[50];int main(){    int n;    int kase=0;    while(~scanf("%d",&n))    {        for(int i=0;i<2*n-1;i++)        {            if(i==0)            {                scanf("%s",a);            }            else            {                scanf("%s",tmp);                for(int i=0;i<strlen(tmp);i++)                {                    a[i]=a[i]^tmp[i];                }            }        }        printf("Scenario #%d\n",++kase);        printf("%s\n\n",a);    }}




0 0
原创粉丝点击