Zoj_3674_search in the Wiki (map)

Source: Internet
Author: User

Search in the Wiki Time limit: 2 Seconds Memory Limit: 65536 KB

As we known, searching in Wiki are an useful-of-the-all who wants to get information. Wiki, a website which allows their users to add, modify, or delete their content via a Web browser, is famous for its huge inf Ormation. You can find almost anything your heard in Wiki.

But sometimes to get into trouble because of these huge information. It's hard-to-find key words from so many characters, which means you'll spend a lot of time understanding what it describ Es. To solve this question, the wiki provides some tips for each word. The tips for one word describe the meaning of this word briefly, so can understand this word quickly through these tip S. A tip consists only from ' a ' to ' Z ' and ' a ' to ' Z '. It ' s a convenient application.

This time you get a task from your teacher to search information for given words. It's a boring work, so you think of Wiki immediately. You get tips for each word from a Wiki, and now you can answer questions the teacher may ask tomorrow morning easily. But to do sure, you decide to test yourself before tomorrow.

You prepare some queries for the test, each query contains some words given before and your should find out all the common Tips of words in this query (A Common tip means all the words in the query has this tip). In order to check the your answer, you need to the write a program now.

Input

There is multiple test cases.

Each case is begins with an integer n (1 <= n <=100), indicating the number of words given. Next n*2 Lines, each of the lines describe a word and its tips. For each of the lines, the first line gives a word (the word is no longer than-characters), and the second line contain s some tips for this word (the string is no longer than characters), and each of the words is separated by one space.

The n*2+2 line contains an integer m (1 <= m <=), indicating the number of queries. Next m lines, each of the contains some words, each of the words is separated by one space. (The string is no longer than characters)

Process to the end of input.

Output

For each query, print one, the common tips of the words in query, and each of the words is separated by one spa Ce. (The common tips should is printed in Alphabet order) If No tips satisfy the previous request, print one line with "no".

Sample Input
4fishagile animalhorseswift animaleaglefierce animalkyuubeealien incubator2fish horse eaglefish horse Eagle Kyuubee
Sample Output

Animalno

Test instructions: First give you a N, then enter the 2*n line, each two rows of a group, the first line of a word, the second line of all the words is the first line of tips, and then give the M query, each query gives the words of common tips. If no is output.

Analysis: Direct map to mess up the line.

Topic Links:

#include <set> #include <map> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <cctype> #include <string> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm>using namespace std;typedef long Long ll;typedef unsigned int uint;typedef unsigned long long ull;const int MAXN = + 5;const int maxm = + 5;int n,m;string sr,ss;ch Ar st[2*maxn];string rstr[2*maxn*maxn];int id1,id2,d1,d2;map<string,int>idx1;map<string,int>idx2;    Vector<int>graph[maxn];int vis[2*maxn*maxn];string ans[2*maxn*maxn];void init () {id1=id2=0;    Idx1.clear ();    Idx2.clear (); for (int i=0;i<maxn;i++) graph[i].clear ();}    int get_idx1 (string name) {if (Idx1.count (name)) return Idx1[name]; IDX1[NAME]=++ID1; return ID1;}    int get_idx2 (string name) {if (Idx2.count (name)) return Idx2[name]; Idx2[name]=++id2; Rstr[id2]=name; return ID2;} void input () {for (int i=1;i<=n;i++) {cin>>sr;        GetChar ();        Gets (ST);        D1=get_idx1 (SR);        int Len=strlen (ST);        Ss.clear ();        St[len]= ";                for (int j=0;j<=len;j++) {if (st[j]== ') {D2=GET_IDX2 (ss);                Graph[d1].push_back (D2);            Ss.clear ();        } else ss+=st[j];    }}}void Solve () {scanf ("%d", &m); GetChar ();        for (int i=1;i<=m;i++) {gets (ST);        int Len=strlen (ST);        St[len]= ";        int cnt=0;        Ss.clear ();        memset (vis,0,sizeof (VIS));                for (int j=0;j<=len;j++) {if (st[j]== ') {d1=get_idx1 (ss);                    for (int k=0;k<graph[d1].size (); k++) {d2=graph[d1][k];                vis[d2]++;                } ss.clear ();            cnt++;        } else ss+=st[j];        } int tot=0;      for (int j=1;j<=id2;j++) {if (vis[j]==cnt) {          Ans[tot].clear ();            ANS[TOT++]=RSTR[J];        }} if (tot==0) printf ("no\n");            else{sort (ans,ans+tot);                for (int j=0;j<tot;j++) {if (j==tot-1) cout<<ans[j]<<endl;            else cout<<ans[j]<< "";        }}}}int Main () {while (scanf ("%d", &n)!=eof) {init ();        Input ();    Solve (); }return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Zoj_3674_search in the Wiki (map)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.