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

ZOJ 3674 模拟的相关文章

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

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 3627#模拟#枚举

Treasure Hunt II Time Limit: 2 Seconds                                     Memory Limit: 65536 KB There are n cities(1, 2, ... ,n) forming a line on the wonderland. city i and city i+1 are adjacent and their distance is 1. Each city has many gold coi

ZOJ 3829 模拟贪心

2014牡丹江现场赛水题 给出波兰式,判断其是否合法,如果不合法有两种操作: 1:任意位置加一个数字或者操作符 2:任意两个位置的元素对调 贪心模拟即可 先判断数字数是否大于操作符数,若不大于 ans+=sum2-sum1+1:新加入的数字全部放到左端. 然后从左到右遍历一遍,存储到当前位置为止,数字数和sum1,和操作数和sum2 若sum2>=1sum1,优先与队尾的数字对调,若没有则sum1++,表示在最左端加一个数字 #include "stdio.h" #include

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来计数每组询问中

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

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

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages