1416: Find the Lost Sock
Time Limit: 2 Sec Memory Limit: 128 MB
[Submit][Status][Web Board]
Description
Alice bought a lot of pairs of socks yesterday. But when she went home, she found that she has lost one of them. Each sock has a name which contains exactly 7 charaters.
Alice wants to know which sock she has lost. Maybe you can help her.
Input
There are multiple cases. The first line containing an integer n (1 <= n <= 1000000) indicates that Alice bought n pairs of socks. For the following 2*n-1 lines, each line is a string with 7 charaters indicating the name of the socks that Alice took back.
Output
The name of the lost sock.
Sample Input
2
aabcdef
bzyxwvu
bzyxwvu
4
aqwerty
easafgh
aqwerty
easdfgh
easdfgh
aqwerty
aqwerty
2
0x0abcd
0ABCDEF
0x0abcd
Sample Output
aabcdef
easafgh
0ABCDEF
ACcode~:
#include <stdio.h>
#include <string.h>
char a[10],b[10];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b));
scanf("%s",a);
for(int i = 1; i < 2*n-1; i++)
{
scanf("%s",b);
for(int i = 0; i < 7; i++)
a[i]^=b[i];
}
printf("%s\n",a);
}
return 0;
}
下面是尝试其他方法做但是没有AC的代码:
1.map计数(稳的时间超限,不过写一下也可以练练手)
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
map<string,int>s;
map<string,int>::iterator it;
string sock;
n = 2*n - 1;
while(n--)
{
cin>>sock;
s[sock]++;
}
for(it = s.begin(); it != s.end(); it++)
{
if(it->second%2!=0)
{
cout<<it->first<<endl;
break;
}
}
}
return 0;
}
2.单纯用string(遇到问题,误解了erase,不过就是更正了也可能时间超限,c++水平不高,暂时不知道怎么更)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string s1,s2;
int main()
{
int n;
while(cin>>n)
{
s1.clear();
s2.clear();
cin>>s1;
for(int i = 1; i < 2*n-1; i++)
{
cin>>s2;
if(s1.find(s2)!=string::npos)
s1.erase(s1.find(s2),s1.find(s2)+7);//这里会有问题,后面部分也被截断(不会自动连起来)
else
s1 += s2;
s2.clear();
}
cout<<s1<<endl;
}
return 0;
}
3.纯c字符串处理函数写~
//(时间超限)
#include <stdio.h>
#include <string.h>
char s1[7000000],s2[10],s3[7000000];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(s1,'\0',sizeof(s1));
scanf("%s",s1);
for(int i = 1; i < 2*n-1; i++)
{
memset(s2,'\0',sizeof(s2));
scanf("%s",s2);
char *p = s1,*q = s2;
if(strstr(p,q))
{
memset(s3,'\0',sizeof(s3));
char *e = strstr(p,q);
strncpy(s3,p,e-p);
strcat(s3,e+7);
memset(s1,'\0',sizeof(s1));
strcat(s1,s3);
}
else
{
strcat(s1,s2);
}
//printf("%s\n",s1);
}
printf("%s\n",s1);
}
return 0;
}