题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3432
题目思路: 其实就是找那个只出现了1次的字符串。 但是如果用数组或者map记录出现的次数,10w的数量会导致超时,学到一种神奇的方式--直接用异或处理
先看代码:
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
string s;
char sock[9];
char ans[9];
int main()
{
int n;
cin>>n;
while(cin>>n)
{
getchar();
n=2*n-1;
gets(ans);
for(int i=1;i<n;i++)
{
gets(sock);
for(int j=0;j<7;j++)
{
ans[j]=ans[j]^sock[j];
}
}
cout<<ans<<endl;
}
}
其中每次输入完n后 因为是cin输入的,还有一个\n需要被吸收掉。
然后对每一位进行异或运算,先转化为对应的asc码的二进制数然后取异或,再将得到的二进制数转化为对应的字符,和异或的效果是一样的--一样的变成空,空异或一个字符还是自己。