ZOJ 3674 Search in the Wiki "C++stl Dafa Simulation"

Source: Internet
Author: User
Tags rehash

Welcome to __xiong 's blog: http://blog.csdn.net/acmore_xiong?viewmode=list
Search in the Wiki
Time limit: 2 Seconds Memory Limit: 65536 KB Link: Just Click me!

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

Analysis:

Test instructions directly skip, this topic is the test of C++stl operation, especially map, according to the test instructions direct simulation is good, it seems that there is no problem solving ideas, water past it, I use two map to achieve bidirectional mapping.

However, Bo Master too Careless, a cin.ignore () misplaced , the result WA to let me Valentine's Day not a little mood, finally, or celebrate their English six smooth passage, a freshman on this end, sophomore refueling! (⊙o⊙) ...

Implementation code:
#include <map> #include <cmath> #include <queue> #include <vector> #include <cstdio># Include <string> #include <cstring> #include <sstream> #include <iostream> #include < algorithm>using namespace std; #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("ou Tput.txt "," w ", stdout) #define CASE (T) int t;for (scanf ("%d ", &t); t--;)//typedef __int64 ll;const int maxn = + 5;struct node{vector<int> Prop;} Species[maxn];map<string, int> hash;map<int, string> rehash;map<string, int> Name;priority_queue <string, Vector<string>, greater<string> > Ans;stringstream sbuf;string str;int CNT, ans[2000];void    Init (const int& N) {hash.clear ();    Name.clear ();    Rehash.clear (); for (int i = 0; i < MAXN; i++) {species[i].    Prop.clear (); } CNT = 0;}    int main () {#ifndef Online_judge FIN; #endif//Online_judge int N, M; while (Cin &GT;&GT        N) {init (n);            for (int i = 1; I <= N; i++) {cin >> str;            NAME[STR] = i;            Cin.ignore ();            Getline (CIN, str);            Sbuf.clear ();            Sbuf << str; while (Sbuf >> str) {if (!                    HASH[STR]) {HASH[STR] = ++cnt;                REHASH[CNT] = str; } Species[i].            Prop.push_back (Hash[str]);        }} cin >> M;        Cin.ignore ();            for (int i = 0; i < M; i++) {sbuf.clear ();            Getline (CIN, str);            Sbuf << str;            int cnt = 0;            memset (ans, 0, sizeof (ans));                while (Sbuf >> str) {int id = name[str]; for (int i = 0; i < Species[id]. Prop.size (); i++) {int propid = Species[id].                    Prop[i];    ans[propid]++;            } cnt++;                    } for (int i = 1; I <= CNT; i++) {if (ans[i] = = CNT) {                Ans.push (Rehash[i]);            }} bool flag = FALSE; while (!                Ans.empty ()) {if (!flag) flag = true;                else cout << "";                str = Ans.top ();                Ans.pop ();            cout << str;            } if (!flag) cout << "NO";        cout << Endl; }} return 0;}

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

ZOJ 3674 Search in the Wiki "C++stl Dafa Simulation"

Related Article

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.