首页 > 代码库 > ZOJ 3674 Search in the Wiki

ZOJ 3674 Search in the Wiki

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674


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

Author: CHEN, Henghong
Contest: ZOJ Monthly, November 2012
Submit    Status


一道模拟题思路不难,但处理起来比较麻烦。。

我用map<string,set<string>> mp 来存储每个关键词所对应的单词。最后再遍历一遍所有需要查询的单词看是否都在mp中即可。

处理的时候要注意去重。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<cstdlib>
#include<map>
using namespace std;
#define CLR(A) memset(A,0,sizeof(A))
map<string,set<string> > mp;
vector<string> g1,g2,ret;
char word[300000];
int main(){
    int n,m,cnt=0;
    while(~scanf("%d",&n)){
        mp.clear();g1.clear();g2.clear();ret.clear();

        //程序读入
        for(int i=0;i<n;i++){
            string a,b;
            cin>>a;
            getchar();
            gets(word);
            istringstream ss(word);
            while(ss>>b){
                g1.push_back(b);
				mp[b].insert(a);
            }
        }
        sort(g1.begin(),g1.end());
        cnt=unique(g1.begin(),g1.end())-g1.begin();

        //query 读入
        scanf("%d",&m);
        getchar();
        string t;
        while(m--){
        	//读入
            gets(word);
            istringstream ss(word);
            g2.clear();ret.clear();
            bool flag=0;
            while(ss>>t){
				g2.push_back(t);
            }

            //枚举处理
            int len=g2.size();
			for(int i=0;i<cnt;i++){
				int j;
				for(j=0;j<len;j++){
					if(mp[g1[i]].find(g2[j])==mp[g1[i]].end()) break;
				}
				if(j==len){
					flag=1;
					ret.push_back(g1[i]);
				}
			}

			//输出
			if(flag==0) cout<<"NO"<<endl;
			else{
				sort(ret.begin(),ret.end());
				for(int k=0;k<ret.size();k++){
					if(k==0) cout<<ret[k];
					else cout<<" "<<ret[k];
				}
				cout<<endl;
			}
        }

    }
    return 0;
}






ZOJ 3674 Search in the Wiki