G

Time Limit: 1000 MS Memory Limit: 131072 K

Description

There are two lists and they may be intersected with each other.
You must judge if they are intersected and find the first node they have in common.

Input

The first line is the number of cases.
Each case consists of two lists.
Each list consists of multiple nodes.
Each node is represented by its address in memory.
So, a list may look like this:
0x233333 0x123456 0xabcdef1 0xffffff nil
nil is the end of the list.
All lists have no cycle.
The length of list is not larger than 4e5.
List can’t be empty.
Warn: huge input.

Output

“No” if they are not intersected.
“Yes Address”, if they are intersected, print “Yes” and the address of their first common node.

Sample Input

2
0x233333 0x123456 0xabcdef1 0xffffff nil
0x999999 0x332232 0xffffff nil
0x233333 0x123456 0xabcdef1 0xffffff nil
0x987654 0xafafaf 0xfffcccc 0xaaface nil

Sample Output

Yes 0xffffff
No
//map//http://www.cnblogs.com/hailexuexi/archive/2012/04/10/2440209.html

#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        map<string,int>p;
        p.clear();
        string s;
        int flag=0;
        while(cin>>s)
        {
            if(s=="nil")
                break;
            p[s]++;
        }
        while(cin>>s)
        {
            if(s=="nil")
                break;
            if(p[s]==1&&!flag)
            {
                cout<<"Yes"<<" "<<s<<endl;
                flag=1;
            }
        }
        if(flag==0)
            cout<<"No"<<endl;
    }
}
原文地址:https://www.cnblogs.com/da-mei/p/9053375.html