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;
}
时间: 2024-08-05 07:07:53

ZOJ 3674 Search in the Wiki的相关文章

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

欢迎关注__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

ZOJ 3674 Search in the Wiki(字典树 + map + vector)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917 题意:每个单词都一些tips单词.先输入n个单词和他们的tips.然后m组查询,每次查询一些单词,按字典序输出这些单词的公有tips.(每个单词都都只包含小写大写字母) 思路:对第i个单词,用vector数组g,g[i]来存这个单词的所有tips.对于所有单词建立字典树,在单词的结尾结点存好该单词的tips在g数组中存的一维下标i.最后用map来计数每组询问中

2014 Super Training #6 F Search in the Wiki --集合取交+暴力

原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下手,不知道怎么实现.后来看了网上的代码,直接用vector和map暴力,用到了set_intersection()函数,之前也听过这个函数,但是一直没写过,于是照着他的代码打了一遍,算是见识一下这个函数了. 代码看一下就能看懂了,关键看自己能不能写出来. 代码: #include <iostream

ZOJ 3674 模拟

[题意]:若干组数据 每组给一个整数n代表n个名词(单词),接下来1-n给出一个单词和一行注释(一行注释由多个字符串组成),然后给出一个整数m,接下来1-m 每行若干个单词来自(1-n),要求出这若干个单词共有的注释字符串并按字典序排列输出,若不存在则输出NO. Sample Input 4 fish agile animal horse swift animal eagle fierce animal Kyuubee alien incubator 2 fish horse eagle fis

ZOJ Monthly, November 2012

A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 10000 + 100; int SG[maxn]; vector<int> g[maxn]; int mex(int u) { //minimal exc

zoj_3674_Search in the Wiki(map)

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 bro

Wiki知识介绍

Wiki简介 Wiki一词来源于夏威夷语的"wee kee wee kee",原本是"快点快点"的意思,被译为"维基"或"维客".一种多人协作的写作工具.Wiki站点可以有多人(甚至任何访问者)维护,每个人都可以发表自己的意见,或者对共同的主题进行扩展或者探讨.Wiki指一种超文本系统.这种超文本系统支持面向社群的协作式写作,同时也包括一组支持这种写作的辅助工具.有人认为,Wiki系统属于一种人类知识网格系统,我们可以在Web

web2py官方文档翻译03

概述 最美代码 web2py官方文档翻译 2016年2月10日 启动 web2py进来二进制包为Windows和Mac OS x包括Python解释器,所以你不需要预装. 还有一个源代码版本运行在Windows,Mac,Linux和其他Unix系统. Windows和mac OS X的二进制版本包括必要的Python解释器. Python源代码包假设已经安装在电脑上. web2py不需要安装. 首先,解压缩下载的zip文件的特定的操作系统和相应的执行 web2py 文件. 在Windows上,运

是神功盖世

http://ypk.39.net/search/all?k=%A1%E8%D1%CE%CB%E1%C7%FA%C2%ED%B6%E0%C4%C4%C0%EF%D3%D0%C2%F4Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A8x http://ypk.39.net/search/all?k=%A8z%C4%C4%C0%EF%C2%F2%D1%CE%CB%E1%C7%FA%C2%ED%B6%E0Q%A3%BA%A3