【题意】:若干组数据 每组给一个整数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 fish horse eagle Kyuubee
Sample Output
animal NO
【知识点】:
模拟
【题解】:
该模拟有如下若干操作
1、分割字符串:我使用strtok函数处理的,这个比直接自己写处理方便省力得多
大概过程如下:
gets(str); char* token = strtok(str, " ");//" "中代表分割标记 while(token){ //当前token就是一个分割的字符 token = strtok(NULL, " ");//寻找下一个字符 }
2、求交集:
看别人的代码,用set_intersection函数,一共五个参数,集合1开头、结尾,集合2开头、结尾,然后是保存交集的集合(数据结构)。
模拟题虽然没有涉及太多的算法知识,但是却挺考验编程功底的。
【代码】:
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #include <queue> 6 #include <stack> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstring> 11 #include <sstream> 12 #include <iostream> 13 #include <algorithm> 14 #include <bitset> 15 #include <climits> 16 using namespace std; 17 18 #define wh while 19 #define inf (int)(~0u/2) 20 #define FOR(i, n) for(int i = 0; i < n; i++) 21 #define FOR1(i, n) for(int i = 1; i < n; i++) 22 #define FOR2(i, n) for(int i = 0; i <= n; i++) 23 #define REP(i,n) for(int i = 1; i <= n; i++) 24 #define FORI(it,n) for(typeof(n.begin()) it = n.begin(); it != n.end(); it++) 25 #define sf scanf 26 #define pf printf 27 #define frs first 28 #define sec second 29 #define psh push_back 30 #define mkp make_pair 31 #define PB(x) push_back(x) 32 #define MP(x, y) make_pair(x, y) 33 #define clr(abc,z) memset(abc,z,sizeof(abc)) 34 #define lt(v) v << 1 35 #define rt(v) v << 1 | 1 36 #define mid ((l + r) >> 1) 37 #define lson l, mid, v << 1 38 #define rson mid + 1, r, v << 1 | 1 39 #define fre freopen("1.txt", "r", stdin) 40 41 typedef long long LL; 42 typedef long double LD; 43 const int maxa = 40; 44 const int maxb = 220; 45 char name[maxa], tip[maxb]; 46 int query[maxb]; 47 48 int N, M, cword, ctip, cq; 49 50 void consume(char ch){ 51 while(getchar() != ch); 52 } 53 int main(){ 54 while(sf("%d", &N) != EOF){ 55 cword = ctip = cq = 0; 56 int a, b; 57 map<string, int> word; 58 map<string, int> tip_id; 59 map<int, string> id_tip; 60 vector<int> G[maxb], t1, t2; 61 REP(i, N){ 62 sf("%s", name); 63 if(!word[name]) 64 word[name] = ++cword; 65 a = word[name]; 66 consume(‘\n‘); 67 gets(tip); 68 char* token = strtok(tip, " "); 69 wh(token != NULL){ 70 if(!tip_id[token]){ 71 tip_id[token] = ++ctip; 72 id_tip[ctip] = token; 73 } 74 b = tip_id[token]; 75 G[a].PB(b); 76 token = strtok(NULL, " "); 77 } 78 sort(G[a].begin(), G[a].end()); 79 } 80 int iter = 0; 81 sf("%d", &M); 82 consume(‘\n‘); 83 wh(M--){ 84 cq = 0; 85 gets(tip); 86 char* token = strtok(tip, " "); 87 wh(token != NULL){ 88 query[cq++] = word[token]; 89 token = strtok(NULL, " "); 90 } 91 t1.clear(); 92 vector<int>::iterator it; 93 for(it = G[query[0]].begin(); it < G[query[0]].end(); it++) 94 t1.PB(*it); 95 for(int j = 1; j < cq; j++){ 96 t2.clear(); 97 set_intersection(t1.begin(), t1.end(), G[query[j]].begin(), G[query[j]].end(), back_inserter(t2)); 98 j++; 99 if(j >= cq){ 100 t1.clear(); 101 for(it = t2.begin(); it < t2.end(); it++) 102 t1.PB(*it); 103 break; 104 } 105 t1.clear(); 106 set_intersection(t2.begin(), t2.end(), G[query[j]].begin(), G[query[j]].end(), back_inserter(t1)); 107 } 108 if(!t1.size()) 109 puts("NO"); 110 else{ 111 set<string> ans; 112 for(it = t1.begin(); it < t1.end(); it++) 113 ans.insert(id_tip[*it]); 114 set<string>::iterator sst; 115 sst = ans.begin(); 116 cout<<*sst; sst++; 117 for(; sst != ans.end(); sst++) 118 cout<<" "<<*sst; 119 puts(""); 120 } 121 } 122 } 123 }
ZOJ 3674 模拟
时间: 2024-10-02 21:23:01