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 ...
随机推荐
- 【转】XMPP_3920_最靠谱的中文翻译文档
CHENYILONG Blog XMPP_3920_最靠谱的中文翻译文档 Fullscreen © chenyilong. Powered by Postach.io Blog
- 对git简单的认识
了解git工作区.暂存区.版本库: 其中,使用 git add .就是将文件添加到了暂存区:而git commit -m ‘desc’:将暂存区的文件添加到版本库: 每次更新项目的步骤: 1)每次更新 ...
- centos7系统下安装配置jdk、tomcat教程
JDK安装与配置 1.下载linux版本的jdk,我下的版本是jdk6.0,下载rpm版本的. 可通过百度搜索文件名:jdk-6u45-linux-x64-rpm.bin下载 也可通过oracle官网 ...
- 输入操作遇到unknown error: cannot focus element
事件背景:写脚本遇到sendkey时报错unknown error: cannot focus element,仔细查了,元素定位什么的都没问题,通过js注入修改数据后,保存成功,但是再进入编辑状态查 ...
- CentOS7上安装与配置Tomcat8与MySQL5.7
一.安装tomcat Tomcat 的安装依赖 JDK,在安装 Tomcat 之前需要先安装 Java JDK.输入命令 java -version,如果显示 JDK 版本,证明已经安装了 JDK.
- 用原生js实现ajax
// 通过createXHR()函数创建一个XHR对象 function createXHR() { if(window.XMLHttpRequest) { // IE7.Firefox.Opera. ...
- generator自动生成代码
idea设置generator自动生成代码: http://blog.csdn.net/sunny243788557/article/details/45166397
- SOA 解惑
SOA 解惑 SOA 不是一种技术,它是一种设计方法.最近一段时间我碰到了很多关于 SOA 的具有误导性的文章.尤其是,有些人混淆了 SOA 和诸如 BPM.ESB 以及复合事件处理 (CEP) 之类 ...
- 删除MySQL binlog日志的方法
对于比较繁忙的OLTP(在线事务处理)系统,由于每天生成日志量大,这些日志如果长时间不清除,将会对磁盘空间带来很大的浪费.因此,定期删除日志是DBA维护MySQL数据库的一个重要工作内容.下面跟大家分 ...
- 《Look Mom, I don’t use Shellcode》议题解析
0x0 前言 <Look Mom, I don’t use Shellcode>是2016年Syscan360上讲过的一个议题,这个议题的副标题是"Browser Exploit ...