ZOJ Monthly, November 2012 - I - Search in the Wiki

Search in the Wiki

Time Limit: 2 Seconds        Memory Limit: 65536 KB

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
4
fish
agile animal
horse
swift animal
eagle
fierce animal
Kyuubee
alien incubator
2
fish horse eagle
fish horse eagle Kyuubee
Sample Output
animal
NO

分析:直接用vector和map暴力,主要用到的是set_intersection()函数

#include<cstring>
#include<cstdio>
#include<set>
#include<string>
#include<vector>
#include<iostream>
#include<map>
#include<iterator>
#include<algorithm>
using namespace std;

char name[35], s[210], tips[210];
int check[110];

int main() {
    int n, m, i, j, a, b, cntw, cntt, cnt, cntc;
    while (scanf("%d", &n) != EOF) {
        cntw = cntt = 0;
        map<string, int> word;
        map<string, int> tip;
        map<int, string> totip;
        vector <int> adj[110];
        vector <int> inter, Inter;
        for (j = 1; j <= n; ++j) {
            scanf("%s", name);
            if (!word[name]) {
                word[name] = ++cntw;
            }
            a = word[name];
            scanf("%*c");
            gets(s);
            //   printf("%s:",name);
            //  puts(s);
            cnt = 0;
            for (i = 0; s[i]; ++i) {
                if (s[i] == 32) {
                    tips[cnt] = 0;
                    if (!tip[tips]) {
                        tip[tips] = ++cntt;
                        totip[cntt] = tips;
                    }
                    b = tip[tips];
                    adj[a].push_back(b);
                    cnt = 0;
                } else
                    tips[cnt++] = s[i];
            }
            tips[cnt] = 0;
            if (!tip[tips]) {
                tip[tips] = ++cntt;
                totip[cntt] = tips;
            }
            b = tip[tips];
            adj[a].push_back(b);
            sort(adj[a].begin(), adj[a].end());
        }
        scanf("%d", &m);
        scanf("%*c");
        while (m--) {
            //      printf("m=%d\n", m);
            cntc = 0;
            gets(s);
            //     puts(s);
            cnt = 0;
            for (i = 0; s[i]; ++i) {
                if (s[i] == 32) {
                    name[cnt] = 0;
                    //  printf("%s\n",name);
                    check[cntc++] = word[name];
                    cnt = 0;
                } else
                    name[cnt++] = s[i];
            }
            name[cnt] = 0;
            check[cntc++] = word[name];
            //      for (i = 0; i < cntc; ++i)
            //         printf("%d:%d\n", i, check[i]);
            inter.clear();
            for (vector<int> ::iterator p = adj[check[0]].begin(); p < adj[check[0]].end(); ++p) {
                inter.push_back(*p);
            }

            for (i = 1; i < cntc; ++i) {
                //      printf("i=%d\n", i);
                Inter.clear();
                //        for (vector<int> ::iterator p = Inter.begin(); p < Inter.end(); ++p)
                //            printf("%d kkkkkk\n", *p); 
                //        for (vector<int> ::iterator p = adj[check[i]].begin(); p < adj[check[i]].end(); ++p)
                //             printf("%d **adj4*\n", *p);
                set_intersection(inter.begin(), inter.end(), adj[check[i]].begin(), adj[check[i]].end(), back_inserter(Inter));
                ++i;

                if (i >= cntc) {
                    inter.clear();
                    for (vector<int> ::iterator p = Inter.begin(); p < Inter.end(); ++p) {
                        inter.push_back(*p);
                        //     printf("%d @@@\n", *p);
                    }
                    break;
                }
                inter.clear();
                set_intersection(Inter.begin(), Inter.end(), adj[check[i]].begin(), adj[check[i]].end(), back_inserter(inter));
            }
            if (!inter.size()) {
                printf("NO\n");
                continue;
            }
            set<string> ans;
            for (vector <int> ::iterator p = inter.begin(); p < inter.end(); ++p) {
                //       printf("%d ^^^*\n", *p);
                ans.insert(totip[*p]);
            }
            set<string> ::iterator u;
            u = ans.begin();
            cout << *u;
            for (++u; u != ans.end(); ++u)
                cout << " " << *u;
            printf("\n");
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/baidongtan/archive/2012/11/30/2796620.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值