SCU – 4490 Lisp em

Lisp em

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

题目链接:https://vjudge.net/problem/SCU-4490

题目大意:t组数据,每组数据输入两行,每行有多个字符串,以nil结束

思路:用map判字符串是否存在,遍历第二行字符串,若存在就记录第一个存在的字符串,并输出

代码:

#include <stdio.h>
#include <string.h>
#include <map>
#include <iostream>
using namespace std;
map<string,int>mp;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        string s;
        mp.clear();
        while(cin>>s&&s!="nil")
            mp[s]=1;
        string m;
        int flag=0;
        while(cin>>s&&s!="nil")
            if(mp[s]==1&&flag==0)
            {
               flag=1;
               m=s;
            }
        if(flag==0)
            printf("No\n");
        else
            cout<<"Yes"<<" "<<m<<endl;
    }
    return 0;
}

 

转载自:https://blog.csdn.net/chimchim04/article/details/88879695

You may also like...