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.
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.
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".
4 fish agile animal horse swift animal eagle fierce animal Kyuubee alien incubator 2 fish horse eagle fish horse eagle Kyuubee
animal NO
题意:输入n n对串 第一行为单词 第二行为单词的解释
然后输入m m个询问 对每个询问 中的单词 找出这些单词解释中的公共字符串
/* map+set一个对应多个 把“解释串”(第二个串) 中的每个单词分开 用每一个单词对应第一个串 如此"解释串"中的某个单词就对应了一个集合 这个集合里存的是第一个串 这个单词就是这些串的公共单词 一开始一个劲的段错误 strcpy(ans[ac],ss[i]); 原因是 ans数组开小了 复制的时候给爆了 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<set> #include<string> #include<map> #include<iostream> using namespace std; char s1[300],s2[300],ss[5000][300]; char s[1000][300],ans[5000][300]; int love(const void *a,const void *b) { return strcmp((char *)a,(char *)b); } int main() { int n,m,i,j,k,cnt,cnt1,ac,flag; map<string,set<string> >mp; while(scanf("%d",&n)!=EOF) { mp.clear(); getchar(); cnt1=0; for(i=1;i<=n;i++) { gets(s1); gets(s2); int t=0; for(j=0;s2[j]!='\0';j++)//把第二个串 分成一个一个的单词放进ss中 { if(s2[j]==' ') { ss[cnt1][t]='\0'; mp[ss[cnt1]].insert(s1); t=0; cnt1++; continue; } ss[cnt1][t++]=s2[j]; } ss[cnt1][t]='\0'; mp[ss[cnt1]].insert(s1); cnt1++; } scanf("%d",&m); getchar(); while(m--) { gets(s2); cnt=0;flag=0; int t=0; for(i=0;s2[i]!='\0';i++) { if(s2[i]==' ') { s[cnt][t]='\0'; cnt++; t=0; continue; } s[cnt][t++]=s2[i]; } s[cnt][t]='\0'; cnt++;//cnt个小串在s中 cnt1个解释串在ss中 ac=0; for(i=0;i<cnt1;i++) { for(j=0;j<cnt;j++) { if(mp[ss[i]].find(s[j])==mp[ss[i]].end()) break; } if(j==cnt) { //ans[ac++]=ss[i]; flag=1; strcpy(ans[ac],ss[i]); ac++; } } if(flag==0) printf("NO\n"); else { qsort(ans,ac,sizeof(ans[0]),love);//排序以便找出重复的 int num=1; for(i=1;i<ac;i++)//去掉重复的 { if(strcmp(ans[i],ans[i-1])!=0) strcpy(ans[num++],ans[i]); } printf("%s",ans[0]); for(i=1;i<num;i++) printf(" %s",ans[i]); printf("\n"); } } } return 0; }