Search in the Wiki(map)

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 Input4fishagile animalhorseswift animaleaglefierce animalKyuubeealien incubator2fish horse eaglefish horse eagle KyuubeeSample OutputanimalNO

题意:首先给你一个n,接下来输入2*n行,每两行一组,第一行一个单词,第二行的所有单词是第一行的tips;然后给出m个查询,每次查询给出单词的共同tips。如果没有输出NO。

分析:直接map乱搞搞就行了。

题目链接:

#include<set>#include<map>#include<ctime>#include<cmath>#include<queue>#include<stack>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 100 + 5;const int maxm = 100 + 5;int n,m;string sr,ss;char st[2*maxn];string rstr[2*maxn*maxn];int id1,id2,d1,d2;map<string,int>idx1;map<string,int>idx2;vector<int>graph[maxn];int vis[2*maxn*maxn];string ans[2*maxn*maxn];void init(){id1=id2=0;idx1.clear();idx2.clear();for(int i=0;i<maxn;i++) graph[i].clear();}int get_idx1(string name){if(idx1.count(name)) return idx1[name];idx1[name]=++id1; return id1;}int get_idx2(string name){if(idx2.count(name)) return idx2[name];idx2[name]=++id2; rstr[id2]=name; return id2;}void input(){for(int i=1;i<=n;i++){cin>>sr;getchar();gets(st);d1=get_idx1(sr);int len=strlen(st);ss.clear();st[len]=' ';for(int j=0;j<=len;j++){if(st[j]==' '){d2=get_idx2(ss);graph[d1].push_back(d2);ss.clear();}else ss+=st[j];}}}void solve(){scanf("%d",&m);getchar();for(int i=1;i<=m;i++){gets(st);int len=strlen(st);st[len]=' ';int cnt=0;ss.clear();memset(vis,0,sizeof(vis));for(int j=0;j<=len;j++){if(st[j]==' '){d1=get_idx1(ss);for(int k=0;k<graph[d1].size();k++){d2=graph[d1][k];vis[d2]++;}ss.clear();cnt++;}else ss+=st[j];}int tot=0;for(int j=1;j<=id2;j++){if(vis[j]==cnt){ans[tot].clear();ans[tot++]=rstr[j];}}if(tot==0) printf("NO\n");else{sort(ans,ans+tot);for(int j=0;j<tot;j++){if(j==tot-1) cout<<ans[j]<<endl;else cout<<ans[j]<<" ";}}}}int main(){while(scanf("%d",&n)!=EOF){init();input();solve();}return 0;}

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

勇于接受自己的不完美,认清自己不足的地方,

Search in the Wiki(map)

相关文章:

你感兴趣的文章:

标签云: