ZOJ 3674 Search in the Wiki 【C++STL大法尽情地模拟】

来源:互联网 发布:入门拳击手套 知乎 编辑:程序博客网 时间:2024/05/22 17:21

欢迎关注__Xiong的博客: http://blog.csdn.net/acmore_xiong?viewmode=list

Search in the Wiki


Time Limit: 2 Seconds      Memory Limit: 65536 KB
链接:Just Click Me!

As we known, Searching in Wiki is an useful way for everyone who wants to get information. Wiki, a website which allows its users to add, modify, or delete its content via a web browser, is famous for its huge information. You can find almost anything you heard in Wiki.

But sometimes you may get into trouble because of these huge information. It's hard to find key words from so many characters, that means you will spend a lot of time understanding what it describes. To solve this question, wiki provides some tips for each word. The tips for one word describe the meaning of this word briefly, so you can understand this word quickly through these tips. A tip consists only of '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 Wiki, and now you can answer questions the teacher may ask tomorrow morning easily. But to make sure, you decide to test yourself before tomorrow.

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

Input

There are multiple test cases.

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

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

Process to the end of input.

Output

For each query, print one line with all the common tips of the words in query, and each two words are separated by one space. (The common tips should be 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

分析:

题意直接略过,这道题目基本就是考对C++STL的操作,尤其是map,照着题意直接模拟就好了,似乎也谈不上什么解题思路,水过去吧,我用到两个map实现双向映射。

然而博主太粗心,一个cin.ignore()放错位置,结果WA到让我七夕情人节没一点心情了,最后,还是庆祝自己英语六级顺利通过,大一就这样结束了,大二加油!(o)

实现代码:

#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("output.txt","w",stdout)#define CASE(T)         int T;for(scanf("%d",&T);T--;)//typedef __int64 LL;const int maxn = 100 + 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 >> 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;}

2 1