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>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
using namespace std;
#define N 10007 char name[],ss[],tips[];
int check[]; int main()
{
int n,m,i,j,a,b;
int cword,ctips,ind,ccheck;
while(scanf("%d",&n)!=EOF)
{
cword = ctips = ;
map<string,int> word;
map<string,int> tip;
map<int,string> atip;
vector<int> G[],K1,K2;
for(j=;j<=n;j++)
{
scanf("%s",name);
if(!word[name])
word[name] = ++cword; //hash
a = word[name];
getchar();
gets(ss);
ind = ;
for(i=;ss[i];i++)
{
if(ss[i] == ' ')
{
tips[ind] = '\0';
if(!tip[tips])
{
tip[tips] = ++ctips;
atip[ctips] = tips;
}
b = tip[tips];
G[a].push_back(b);
ind = ;
}
else
tips[ind++] = ss[i];
}
tips[ind] = ;
if(!tip[tips])
{
tip[tips] = ++ctips;
atip[ctips] = tips;
}
b = tip[tips];
G[a].push_back(b);
sort(G[a].begin(),G[a].end());
}
scanf("%d",&m);
getchar();
while(m--)
{
ccheck = ;
gets(ss);
ind = ;
for(i=;ss[i];i++)
{
if(ss[i] == ' ')
{
name[ind] = ;
check[ccheck++] = word[name];
ind = ;
}
else
name[ind++] = ss[i];
}
name[ind] = ;
check[ccheck++] = word[name];
K1.clear();
vector<int> ::iterator it;
for(it=G[check[]].begin();it<G[check[]].end();it++)
K1.push_back(*it);
for(i=;i<ccheck;i++)
{
K2.clear();
set_intersection(K1.begin(),K1.end(),G[check[i]].begin(),G[check[i]].end(),back_inserter(K2));
i++;
if(i >= ccheck) //last one
{
K1.clear();
for(it=K2.begin();it<K2.end();it++)
K1.push_back(*it);
break;
}
K1.clear();
set_intersection(K2.begin(),K2.end(),G[check[i]].begin(),G[check[i]].end(),back_inserter(K1));
}
//最终结果看K1
if(!K1.size())
{
puts("NO");
continue;
}
set<string> ans;
for(it=K1.begin();it<K1.end();it++)
ans.insert(atip[*it]);
set<string>::iterator st;
st = ans.begin();
cout<<*st;
for(st++;st!=ans.end();st++)
cout<<" "<<*st;
puts("");
}
}
return ;
}
2014 Super Training #6 F Search in the Wiki --集合取交+暴力的更多相关文章
- 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂
原题:ZOJ 3774 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- 2014 Super Training #2 F The Bridges of Kolsberg --DP
原题:UVA 1172 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 2014 Super Training #1 F Passage 概率DP
原题: HDU 3366 http://acm.hdu.edu.cn/showproblem.php?pid=3366 本来用贪心去做,怎么都WA,后来看网上原来是一个DP题. 首先按P/Q来做排 ...
- 2014 Super Training #7 E Calculate the Function --矩阵+线段树
原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...
- 2014 Super Training #2 C Robotruck --单调队列优化DP
原题: UVA 1169 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- 2014 Super Training #1 B Fix 状压DP
原题: HDU 3362 http://acm.hdu.edu.cn/showproblem.php?pid=3362 开始准备贪心搞,结果发现太难了,一直都没做出来.后来才知道要用状压DP. 题意: ...
- 2014 Super Training #8 B Consecutive Blocks --排序+贪心
当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...
- 2014 Super Training #8 A Gears --并查集
题意: 有N个齿轮,三种操作1.操作L x y:把齿轮x,y链接,若x,y已经属于某个齿轮组中,则这两组也会合并.2.操作Q x y:询问x,y旋转方向是否相同(等价于齿轮x,y的相对距离的奇偶性). ...
随机推荐
- 基于进程的Quartz.NET管理系统QuartzService(一)
需求 在处理定时任务大家可能都用过Quartz.NET,但在生产环境中大家肯定也遇到过如下的问题: 发布的时候需要停掉所有的Job,再整个一起打包发布 没有管理界面(其实在github也有几个这方面的 ...
- Introduction to ASP.NET 5
ASP.NET 5 is a significant redesign of ASP.NET. This topic introduces the new concepts in ASP.NET 5 ...
- Win10 FaceAPI小demo开发问题汇总
Win10 FaceAPI小demo开发问题汇总 最近使用微软牛津计划做一个小demo,使用FaceAPI做一个小应用,实现刷脸的功能.开发的过程中用到几个问题,具体如下: Stream 与IRand ...
- 转发离线安装 Android Studio 更新
1.在线更新 随着 Android Studio 的越来越完善与流行,无论从功能性,还是性能上,它正在成为广大 Android 开发者的首选.但是因为总所周知墙的原因,我们在 Android Stud ...
- Android 购物车功能的实现
首先,众所周知,ListView是Android最常用的控件,可以说是最简单的控件,也可以说是最复杂的控件. 作为一个Android初级开发者,可能会简单的ListView展示图文信息. 作为一个有一 ...
- 内存管理2(主讲MRR)
内存管理2 我们讨论过properties 后,所有的内存管理系统都是通过控制所有对象的生命周期来减少内存的占用.iOS和OS X应用程序完成这些是通过对象拥有者来实现的,它保证了只要对象使用就会存在 ...
- 【原】log4cplus使用说明
网上关于开源日志工具log4cplus的说明有很多,但大多略显复杂,本文主要从实用的角度,介绍一种最简单而且又实用的方法.本文的方法已经足够满足实际工程中的使用需求,而且不需要很复杂的流程,可以实现. ...
- javascript 调试技巧
不用alert,用console.log() <!DOCTYPE html> <html> <head> <script type="text/ja ...
- Asp.net 页面访问模板页的属性
首先 页面需要添加下面一段代码 <%@ MasterType VirtualPath="~/User/User.Master" %> 添加的位置如图 这样就可以在这个页 ...
- 关于Redis中交互的过程
一.Redis启动 加载配置(命令行或者配置文件) 启动TCP监听,客户端的列表保存在redisserver的clients中 启动AE Event Loop事件,异步处理客户请求 事件处理器的主循环 ...