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 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

分析:

题意直接略过,这道题目基本就是考对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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-29 09:37:37

ZOJ 3674 Search in the Wiki 【C++STL大法尽情地模拟】的相关文章

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 whi

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

(STL大法) hdu 3283

The Next Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 850    Accepted Submission(s): 597 Problem Description For this problem, you will write a program that takes a (possibly long

信STL,得永生(STL大法好)

STL大法好! 当然,是不可能得永生的,但它可以延长你的在役时间 最近总结了一些STL的神奇用法,C++的STL和作弊器一样,简直kongbu...... 所以说,我们一起来看看STL的用法吧! 目录: 1.vector 2.stack 3.queue&priority_queue 4.string 5.map 6.set&multiset 7.list 8.pair 9.bitset 10.algorithm 当然,这只会是一个大致的讲解,不会非常详细 vector vector,就是不

数据结构 算法3.4(栈的应用) 表达式求值(stl版and数组模拟版)

问题是 输入一串表达式 其中包括 数字 和各种运算符(   +,-,*,/,(,) ) 求它的值 如 4+(5+2*7)*3 stl版: #include<iostream> #include<cstdio> #include<cstring> #include<stack> using namespace std; int operate(int a,char op,int b) { if(op=='+') return a+b; if(op=='-')

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