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的相对距离的奇偶性). ...
随机推荐
- [maven] 生命周期和插件
maven生命周期和插件 生命周期 maven的生命周期有三套,互相独立.每个生命周期含有不同阶段,常用如下 clean 清理项目 pre-clean 执行清理前需要完成的工作 clean 清理上一次 ...
- System.Data.Dbtype转换为System.Data.SqlDbType
最近在做一些OM Mapping的准备工作,新学了一招. 如果要将System.Data.Dbtype转换为System.Data.SqlDbType,以前以为要写Switch Case语句.其实有很 ...
- 【转】深入浅出Android Support Annotation
[转自]http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0427/2797.html http://www.flysnow.org/201 ...
- 用doxygen+graphviz自动化生成代码文档(附详细教程)
一.引子 用这两个工具可以自动的遍历代码,并且产生代码文档,我们先来看看效果,然后放出这两个工具的下载地址. 二.工具的下载地址 doxygen:http://www.stack.nl/~dimitr ...
- UWP开发-HTTP详解
HTTP作为一个基础功能,有必要介绍下在UWP下的使用方法. 一.Get请求: 一般我们用到的是GetAsync方法 public static async Task Gets(Uri uri) { ...
- Emacs常用命令汇总
注意:以下命令中标注的按键,大写的C代表Control,在键盘上通常是Ctrl键,而M代表Meta,在键盘上通常是Alt键,S则代表Shift,在键盘上通常是Shift键,也就是 C Control ...
- IOS Quartz2D简介
Quartz2D 简介( 后续会有相关应用) 第一部分 绘制直线 代码示例: - (void)drawRect:(CGRect)rect{ //获取图形上下文 CGContextRef cxConte ...
- .NET下的并行开发
并行开发一直是程序员在开发项目中遇到的一道坎,但为了迎合硬件的升级,面对高端多核的处理器,并行编程势在必行.在.NET平台下的开发支持并行模式,下面用一个实际项目说明并行的高效率和神奇之处. 在优化中 ...
- iOS之UI--CAGradientLayer
1.CAGradientLayer 简介 如果说CAShapeLayer是用于提供设置形状的,那么CAGradientLayer是用于提供设置颜色的 英语单词:Gradient:梯度,渐变 那么Gra ...
- C语言--static全局使用示例
前言:看到很多使用Objective-C开发IOS的大牛,有时候会使用static全局变量,相比之下,我却很少用这个,从而很少对其有着比较有实质意义的理解,甚至更别说运用它了. 今天,经过一番思考和自 ...