ZOJ 3674 Search in the Wiki

來源:互聯網
上載者:User

標籤:類比   acm   zoj   map   rongqi   

題目連結:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674


Search in the WikiTime 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
4fishagile animalhorseswift animaleaglefierce animalKyuubeealien incubator2fish horse eaglefish horse eagle Kyuubee
Sample Output
animalNO
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.