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 ...
随机推荐
- python基础学习之类的属性 增删改查
类中的属性如何在类外部使用代码进行增删改查呢 增加.改变: setattr内置函数以及 __setattr__魔法方法 class A: aaa = '疏楼龙宿' a = A() setattr(a, ...
- linux下 > /dev/null 2 > &1 的意思和如何在后台启动进程
一.几个基本符号及其含义 之前看到别人写的一个shell脚本,有一个命令是:rm -f ${src_tmp_file} > /dev/null 2>&1 现在大概明白是什么意思了 ...
- Tomcat搭建配置
Tomcat是Apache软件基金会( Apache Software Foundation )的Jakarta项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发而成.受Java ...
- MyBatis工程搭建&MyBatis实现Mapper配置查询
一.MyMyBatis工程搭建 新建Maven项目:mybatis-demo 准备数据源 1 # 删除mybatis_demo数据库 2 drop database if exists mybatis ...
- 使用docker搭建sonarqube
sonarqube是一款代码质量检查工具,使用sonar扫描我们写过的代码,可以有助于检查出代码的bug.规范性和健壮性,有助于提高我们的代码质量. 一.安装docker 安装完成之后,命令行输入 d ...
- vue-i18n 国际化语言切换
vue-i18n 用于前端vue项目中,需要多语言切换的场景 安装方法(npm) npm install vue-i18n 简单使用 1.在vue项目的main.ts文件中实例化 i18n imp ...
- IndexError: list index out of range Python常见错误
引用超过list最大索引,此错误非常常见,注意列表的元素个数 ----------------------------------------------
- JS复制文本到粘贴板,前端H5移动端点击按钮复制文本
<span id="codeNum">FTYHDSDW</span> <span class=" code-btn" id=&qu ...
- (三)Struts2的Action(简单讲解版)
Actions是Struts2框架的核心,因为它们适用于任何MVC(Model View Controller)框架. 每个URL映射到特定的action,其提供处理来自用户的请求所需的处理逻辑.但a ...
- hello world!goodbye world~
我有个朋友,做ios开发做了5年,年前回家转行赚大钱去了,这个标题,其实就是因他而生. 我本人做的.net开发,也差不多快5年时间了,在这个时候暂借博客园这个平台说几句心里话,骚了勿喷:) 其实我是个 ...