pat advanced 1139. First Contact (30)
- 解法暴力
- 因为有 0000, -0000 这样的数据,所以用字符串处理
- 同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
//解法暴力
//因为有 0000, -0000 这样的数据,所以用字符串处理
//同性的时候,遍历好朋友时会直接遍历到对方,这个时候应该continue
int n, m;
unordered_map<string, unordered_set<string>> relations;
bool is_same_gender(string id1, string id2)
{
if ((id1[0] == '-' && id2[0] == '-') || (id1[0] != '-' && id2[0] != '-')) return true;
return false;
}
struct result
{
string id1;
string id2;
result(string _x, string _y)
{
if (_x[0] == '-') id1 = _x.substr(1);
else id1 = _x;
if (_y[0] == '-') id2 = _y.substr(1);
else id2 = _y;
}
void print()
{
cout << id1 << " " << id2 << endl;
}
bool operator<(const result&r)const
{
return id1 == r.id1 ? id2 < r.id2: id1 < r.id1 ;
}
};
int main()
{
while (scanf("%d %d", &n, &m) != EOF)
{
char id1[6], id2[6];
relations.clear();
while (m--)
{
scanf("%s %s", id1, id2);
relations[id1].insert(id2);
relations[id2].insert(id1);
}
int q;
scanf("%d", &q);
while (q--)
{
vector<result> res; res.clear();
scanf("%s %s", id1, id2);
for (unordered_set<string>::iterator i = relations[id1].begin(); i != relations[id1].end(); i++)
{
if (!is_same_gender(id1, (*i)) || (*i) == string(id2)) continue;
for (unordered_set<string>::iterator j = relations[id2].begin(); j != relations[id2].end(); j++)
{
if (!is_same_gender(id2, (*j)) || (*j) == string(id1)) continue;
if (relations[(*i)].find(*j) != relations[(*i)].end())
{
res.push_back(result((*i), (*j)));
}
}
}
sort(res.begin(), res.end());
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++)
res[i].print();
}
}
return 0;
}
pat advanced 1139. First Contact (30)的更多相关文章
- PAT 1139 First Contact[难][模拟]
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- PAT甲级1139 First Contact
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
- PAT (Advanced Level) 1057. Stack (30)
树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- PAT Advanced 1022 Digital Library (30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PAT Advanced 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT Advanced 1072 Gas Station (30) [Dijkstra算法]
题目 A gas station has to be built at such a location that the minimum distance between the station an ...
- PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]
题目 Input our current position and a destination, an online map can recommend several paths. Now your ...
- PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
随机推荐
- Mac下配置环境变量重启后不生效解决(.bash_profile vs .bashrc)(bash/zsh下不加载.bashrc问题解决)
参考上一篇文章说明:http://www.cnblogs.com/EasonJim/p/6283094.html 得知加载顺序如下: /etc/profile /etc/paths ~/.bash_p ...
- 求二叉树中第K层结点的个数
一,问题描述 构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第K层中的结点个数(根结点为第0层) 二,二叉树的构建 定义一个BinaryTree类来表示二叉树,二叉树BinaryTree 又是由 ...
- 对 JavaScript 下 namespace 功能的简单分析
前些天在剥离 百度随心听 的播放器引擎时,看到了一个namespace方法,觉得新奇,当然只是对于我自己而言,我入门js不久,经验尚浅.之前看到网易还是新浪还是什么什么网站来着,也是用类似这种东西的, ...
- Linux 静态库与动态库
静态库(.a) 一个deal.c usedeal.c 重点 1. gcc -c deal.c 生成 deal.o 2. ar -rsv libdeal.a deal.o 生成 libdeal.a ...
- 20155303 2016-2017-2 《Java程序设计》第六周学习总结
20155303 2016-2017-2 <Java程序设计>第六周学习总结 课堂笔记 高效学习法推荐 看视频学习(2h)→ 以代码为中心看课本,思考运行结果并验证(3h)→ 课后作业验证 ...
- iOS中UITableView和UICollectionView的默认空态页
项目中想实现空态页风格统一控制的效果,就封装了一个默认空态页,使用的技术点有:1 方法替换 ,2 给分类(Category)添加属性. 我们知道,扩展(extension)可以给类添加私有变量和方法. ...
- Comparable和Comparator的区别&Collections.sort的两种用法
在Java集合的学习中,我们明白了: 看到tree,可以按顺序进行排列,就要想到两个接口.Comparable(集合中元素实现这个接口,元素自身具备可比性),Comparator(比较器,传入容器构造 ...
- 从消费者角度评估RestFul的意义
相关博文: 从消费者角度评估RestFul的意义 SpringBoot 构建RestFul API 含单元测试 REST是目前业界相当火热的术语,似乎发布的API不带个REST前缀,你都不好意思和别人 ...
- SpringMvc定时器任务
在最近的工作中,涉及到一个定时任务,由于以前对springMVC使用较少,所以,上网找了一点资料.这个demo感觉挺好,推荐给大家. 使用到的JAR文件: aopalliance-1.0.jarcom ...
- C# 各版本新特性
C# 2.0 泛型(Generics) 泛型是CLR 2.0中引入的最重要的新特性,使得可以在类.方法中对使用的类型进行参数化. 例如,这里定义了一个泛型类: class MyCollection&l ...