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 ...
随机推荐
- VMware Linux 下 Nginx 安装配置 (一)
资源准备 1. pcre-8.34.tar.gz: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 2. zlib-1.2.8.tar.g ...
- Anaconda+django写出第一个web app(六)
今天学习如何写一个注册用户的界面. 上一节的导航栏中我们修改了导航栏右侧的文字为register并将路径设置为/register,内容如下: <li><a href="/r ...
- 记关于webpack4下css提取打包去重复的那些事
注意使用vue-cli3(webpack4),默认小于30k不会抽取为公共文件,包括css和js,已测试 经过2天的填坑,现在终于有点成果 环境webpack4.6 + html-webpack-pl ...
- 第八集:魔法阵 NTT求循环卷积
题目来源:http://www.fjutacm.com/Problem.jsp?pid=3283 题意:给两串长度为n的数组a和b,视为环,a和b可以在任意位置开始互相匹配得到这个函数的值,求这个函数 ...
- Android启动过程
1.背景知识 Init进程是Linux环境下非常重要的一个进程,而Zygote进程是J ...
- python自动安装mysql5.7【转】
#!/usr/bin/env python import os import sys import re base_dir = '/opt/software/mysql-5.7.17-linux-gl ...
- React-Native 之 TabBarIOS
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- 小白学习安全测试(一)——Http协议基础
Http协议基础 Web技术发展[http://www.cnblogs.com/ProgrammerGE/articles/1824657.html] 静态WEB[网页] 动态WEB 属于一种应用程序 ...
- 谈谈如何查看Android项目方法数
我们都知道,Android App的方法数是有天花板的,在方法数达到65536时,就会出现打包异常,这个时候,我们需要去除一些不需要的三方工具包,或者采用多Dex技术分包,都能达到正常打包的效果. 可 ...
- dede列表页读取当前栏目名称
list或者arclist之内使用[field:typename/]之外使用{dede:field name='typename'/}