M - Lisp em SCU - 4490

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

Author

qw4990

描述


有两个可能intersected列表和他们的彼此。

你必须intersected法官如果他们发现他们有一个与第一节点中常见。


输入


第一行是在数量的情况下。

每个案例包括两个列表。

每个列表包括多个节点。

每个节点是由在内存中的地址的代表。

所以,这个列表可能看起来像:

0x233333 0x123456 0xabcdef1 0xffffff无

列表是无尽头的。

在所有的列表有一个周期。

更大的长度比4e5列表”。

can’t是空的列表。

警告:巨大的投入。

输出


“如果他们不intersected”。

“是的,他们是intersected地址”,如果“是”,打印的地址和他们的第一个公共节点。

思路:用map来查找,第一行出现的地址让它的值加一,第二行如果发现再次出现就输出它

一开始用set做,现在还不知道错那.....

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       string a;
       int b=0;
       map<string,int> s;
       int flag=0;
       while(cin>>a)
       {
           if(a=="nil")
            break;
           s[a]++;
       }
       while(cin>>a)
       {
           if(a=="nil")
            break;
           if(s[a]==1&&b==0)
           {
            cout<<"Yes "<<a<<endl;
            b=1;
           }
       }
       if(b==0)
        cout<<"No"<<endl;
   }
}
 

猜你喜欢

转载自blog.csdn.net/weixin_43740907/article/details/88901025
EM