1. 程式人生 > >1416: Find the Lost Sock(用異或才AC)

1416: Find the Lost Sock(用異或才AC)

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;
    }