hdu - 1716 排列2 (使用set对全排列结果去重)
题意很简单,只是有几个细节要注意,首先就是一次只是输入四个数字、输出结果要从小到大(进行全排列之前要进行排序)、题目要求千位数相同的在一行,中间使用空格隔开(第二次在输出的时候判断上一次记录的千位数是不是和这一次的相等,相等就输出空格,不等就输出换行)、每组输出数据间加一个空行,最后一组后不可以有空行(第二次之后输入的不是0 0 0 0就输出一个空行就可以了,否则break)。
还要两个隐藏的条件:①输出的时候前导0的一组不要;②输入数据可以有相同的数字,输出的时候要进行去重;
对于本题来说,由于时间复杂度不是很大,可以使用一个容器(vector,arraylist)把结果存起来,在存的时候或者是输出的时候去遍历整个容器是不是有当前的这组数据,再进行相关处理。
我这里偷了一下懒(实际上是想熟悉一下set),不得说set确实挺好用的。全排列的知识点请参考:(默认从小到大的顺序储存)
http://www.cnblogs.com/Ddlm2wxm/p/6637122.html
具体输出格式以及代码控制见代码:
1 #include <iostream>
2 #include <algorithm>
3 #include <string>
4 #include <cstring>
5 #include <cstdio>
6 #include <set>
7 using namespace std;
8
9 char s[8];
10 char res[8];
11 bool used[10];
12 char c;
13 bool f;
14 set<string> se;
15
16 void print () {
17
18 for (set<string>::iterator p = se.begin(); p != se.end(); ++p) {
19 if (f) {
20 if (c == (*p)[0]) {
21 cout << ' ';
22 } else {
23 cout << endl;
24 }
25 }
26 while ((*p)[0] == '0') {
27 p++;
28 }
29 cout << *p;
30 c = (*p)[0];
31 f = true;
32 }
33 }
34
35 void perm (int pos, int n) {
36 if (pos == n) {
37 se.insert (res);
38 return ;
39 }
40 for (int i = 0; i < n; ++i) {
41 if (!used[i]) {
42 res[pos] = s[i];
43 used[i] = true;
44 perm (pos+1, n);
45 used[i] = false;
46 }
47 }
48 }
49
50 int main () {
51 bool a = false;
52 while (1) {
53 f = false;
54 for (int i = 0; i < 4; ++i) {
55 cin >> s[i];
56 }
57 if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0') {
58 break;
59 }
60 if (a) {
61 cout << endl;
62 }
63 a = true;
64 sort (s, s+4);
65 memset(used, false, sizeof (used));
66 perm(0, 4);
67 print();
68 se.clear();
69 cout << endl;
70 }
71 return 0;
72 }
在这里添加一点使用set求交集、并集、差集的几个函数的笔记:
1 #include <iostream>
2 #include <algorithm>
3 #include <set>
4 #include <cstdio>
5 using namespace std;
6
7 int main () {
8 set<int> a, b, c;
9 int n;
10 cout << "A:" << endl;
11 for (int i = 0; i < 3; ++i) {
12 cin >> n;
13 a.insert (n);
14 }
15 cout << "B:" << endl;
16 for (int i = 0; i < 4; ++i) {
17 cin >> n;
18 b.insert (n);
19 }
20
21 c.clear();
22 set_intersection (a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
23 for (set<int>::iterator p = c.begin(); p != c.end(); ++p) {
24 cout << *p << ' ';
25 }
26 cout << endl;
27
28 c.clear();
29 set_union (a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
30 for (set<int>::iterator p = c.begin(); p != c.end(); ++p) {
31 cout << *p << ' ';
32 }
33 cout << endl;
34
35 c.clear();
36 set_difference (a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
37 for (set<int>::iterator p = c.begin(); p != c.end(); ++p) {
38 cout << *p << ' ';
39 }
40 cout << endl;
41 return 0;
42 }
43
44 /*
45 A:
46 1 2 4
47 B:
48 3 2 1 3
49 1 2
50 1 2 3 4
51 4
52 */
C++对学习算法来说,确实很方便,一方面节省了很多时间,另一方面也会忘记最基础算法的实现,比如想到交换,都可以一行swap(a, b);实现,都不会想到去另外定义一个变量t,更不会想到定义一个函数,使用指针的方式从地址上实现数值的交换。
所以,先定个小目标:STL既要会用,更要学会看源代码。
hdu - 1716 排列2 (使用set对全排列结果去重)的更多相关文章
- hdu 1716 排列2(DFS搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Time Limit: 1000/1000 MS (Java/Others) Me ...
- hdu 1716 排列
题目 这道题是全排列问题,主要注意的是格式问题.觉得下面这种写法最为巧妙 #include <cstdio> #include <iostream> #include < ...
- HDU - 1716 排列2 水题
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 1716 排列2
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 杭电的题,输出格式卡的很严。HDU 1716 排列2
题很简单,一开始写代码,是用整数的格式写的,怎么跑都不对,就以为算法错了,去看大佬们的算法STL全排列:next_permutation(); 又双叒叕写了好几遍,PE了将近次,直到跑了大佬代码发现, ...
- HDU 1716 排列2 (格式问题+排列)
题意:. 析:我们完全可以STL里面的函数next_permutation(),然后方便,又简单,这个题坑就是在格式上. 行末不能有空格,结尾不能有空行,不大好控制,必须控制好第一次数. 这个题本应该 ...
- hdu 1716(dfs)
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Problem Description Ray又对数字的列产生了兴趣:现 ...
- STL训练 HDU - 1716 Ray又对数字的列产生了兴趣:
HDU - 1716 Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(0&l ...
- HDU 1716:排列2(全排列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
随机推荐
- 安装JDK步骤,配置环境变量
DK是Java语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具.所以今天教一 ...
- hdu 4622 (hash+“map”)
题目链接:https://vjudge.net/problem/HDU-4622 题意:给定t组字符串每组m条询问--求问每条询问区间内有多少不同的子串. 题解:把每个询问区间的字符串hash一下存图 ...
- CodeForces - 1360C
C. Similar Pairs time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 练习使用Unicorn、Capstone
Unicorn是一个轻量级的多平台,多体系结构的CPU仿真器框架.官网:http://www.unicorn-engine.org/ Capstone是一个轻量级的多平台,多体系结构的反汇编框架.官网 ...
- 攻防世界 reverse crazy
crazy 百越杯2018 查看main函数: int __cdecl main(int argc, const char **argv, const char **envp) { __int64 v ...
- maven-plugin-shade 详解
一.介绍 [1] This plugin provides the capability to package the artifact in an uber-jar, including its d ...
- 仅仅使用Google就完成了人生第一次破解
2021年2月6日21:17:09 begin 起因 在异乡的打工人,不善言谈,幸有一老同学,周末常邀吃饭,感恩之心铭记于心.她结婚时,为表心意欲做视频,视频需要制作字幕,搜索之,偶遇一字幕软件,但是 ...
- Flex属性你真的搞清楚了吗?我深表怀疑
背景 在使用弹性布局实现两侧宽度固定,中间宽度自适应的效果时,发现自己理解的和实际效果不一致,所以亲自实践验证了一个flex属性的诸多场景的表现,不仅解开了我之前使用过程遇到的疑惑,而且发现了许多自己 ...
- Python信息搜集
1.IP查询 IP查询是通过当前所获取到的URL去查询对应IP地址的过程.可以应用socket库函数中的gethostbuname()获取域名所对应的IP值,代码如下: 查询域名www.biadu.c ...
- vue-cli2 项目中使用node-sass
公司的项目,换了个电脑要重新安装一下依赖,但是直接npm install的时候报错了,提示node-sass未安装成功. 然后直接npn install node-sass --save 的时候还是下 ...