sicily 1198. Substring (递归全排列+排序)
Description
Dr lee cuts a string S into N pieces,s[1],…,s[N].
Now, Dr lee gives you these N sub-strings: s[1],…s[N]. There might be several possibilities that the string S could be. For example, if Dr. lee gives you three sub-strings {“a”,“ab”,”ac”}, the string S could be “aabac”,”aacab”,”abaac”,…
Your task is to output the lexicographically smallest S.
Input
The first line of the input is a positive integer T. T is the number of the test cases followed.
The first line of each test case is a positive integer N (1 <=N<= 8 ) which represents the number of sub-strings. After that, N lines followed. The i-th line is the i-th sub-string s[i]. Assume that the length of each sub-string is positive and less than 100.
Output
The output of each test is the lexicographically smallest S. No redundant spaces are needed.
给一堆子串,求这堆子串的排列里字典序最小的那个。因为题目数据量很小所以可以直接全排列+排序,复杂度是O(n!)(这么暴力真是对不起……
为了方便用递归来求全排列(同样,反正数据量小我怕谁2333),stl的set自带有序所以就顺手用了。
#include<string>
#include<cstring>
#include<iostream>
#include<set>
using namespace std; set<string> permutation;
int len;
string substring[];
string fullstr;
bool visited[]; void recur(int cur) {
if (cur == len) {
// compelete a full string consist of len substrings
permutation.insert(fullstr);
} else {
for (int i = ; i < len; ++i) {
if (!visited[i]) {
int lastEnd = fullstr.size(); // add another substring
fullstr += substring[i];
visited[i] = true; recur(cur + ); // remove the substring added in this level
int curEnd = fullstr.size();
fullstr.erase(lastEnd, curEnd);
visited[i] = false;
}
}
}
} int main(void) {
#ifdef JDEBUG
freopen("1198.in", "r", stdin);
freopen("1198.out", "w", stdout);
#endif
int t; cin >> t;
while(t--) {
cin >> len; // initialization
fullstr.clear();
permutation.clear();
memset(visited, false, sizeof(visited)); for (int i = ; i < len; ++i) {
cin >> substring[i];
} recur(); // use the lexicographically smallest full string
cout << *(permutation.begin()) << '\n';
}
return ;
}
sicily 1198. Substring (递归全排列+排序)的更多相关文章
- python非递归全排列
刚刚开始学习python,按照廖雪峰的网站看的,当前看到了函数这一节.结合数组操作,写了个非递归的全排列生成.原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位 ...
- C#实现(递归和非递归)高速排序和简单排序等一系列排序算法
本人由于近期工作用到了一些排序算法.就把几个简单的排序算法.想冒泡排序,选择排序,插入排序.奇偶排序和高速排序等整理了出来,代码用C#代码实现,而且通过了測试.希望能给大家提供參考. ...
- 非递归全排列 python实现
python algorithm 全排列(Permutation) 排列(英语:Permutation)是将相异物件或符号根据确定的顺序重排.每个顺序都称作一个排列.例如,从一到六的数字有720种排列 ...
- 牛客多校第十场 E Hilbert Sort 递归,排序
题意: 给你一个方阵,再在方阵上给定一些点,按照希尔伯特曲线经过的先后顺序为这些点排序 题解: 定义好比较函数后直接调用排序算法即可. 希尔伯特曲线本来就是用于二维到一维的映射的,因此我们可以考虑对于 ...
- Codeforces 919D:Substring(拓扑排序+DP)
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...
- Codeforces 919D Substring 【拓扑排序】+【DP】
<题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...
- sicily 1046. Plane Spotting(排序求topN)
DescriptionCraig is fond of planes. Making photographs of planes forms a major part of his daily lif ...
- Codefroces 919D Substring(拓扑排序+DP)
题目链接:http://codeforces.com/problemset/problem/919/D 题目大意:给你一张有向图,给你每个顶点上的字母和一些边,让你找出一条路径,路径上的相同字母数最多 ...
- 递归merge排序
package sort; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scan ...
随机推荐
- Hadoop生态圈-HBase的HFile创建方式
Hadoop生态圈-HBase的HFile创建方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 废话不多说,直接上代码,想说的话都在代码的注释里面. 一.环境准备 list cr ...
- Excel 中 VLOOKUP() 函数小结
应用场景: 数据仓库上游源系统的数据库表变更,现在需要拆分一部分数据出来,单独放到一张新表中.假设原表为A,新表为B,B表和A表结构大部分一样,只有字段的前缀不同,那么我们如何找出到底有哪些字段不同呢 ...
- jQuery合并单元格以及还原重置
一.合并rowspan jQuery.fn.rowspan = function(colIdx) { return this.each(function(){ var that; $('tr', th ...
- jdk1.8.0_45源码解读——LinkedList的实现
jdk1.8.0_45源码解读——LinkedList的实现 一.LinkedList概述 LinkedList是List和Deque接口的双向链表的实现.实现了所有可选列表操作,并允许包括null值 ...
- anonymous namespace V.S. static variant
[anonymous namespace V.S. static variant] 在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做为函数名或者全局变量名,则在链 ...
- HDU 1422 重温世界杯 DP题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1422 解题报告:DP题,要使旅行的城市最多,关键是要选出一个城市作为开始,以这个城市作为开始的城市时, ...
- 【leetcode 简单】 第七十七题 单词模式
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式. 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向 ...
- php empty()与isset()
empty() : 检查一个变量是否为空. 判断一个变量是否被认为是空的.当一个变量并不存在,或者它的值等同于FALSE,那么它会被认为不存在.如果变量不存在的话,empty()并不会产生警告. em ...
- WeX5入门之欢乐捕鱼打包
一.下载欢乐捕鱼的素材包 https://files.cnblogs.com/files/wordblog/%E7%B4%A0%E6%9D%90.zip 二.把欢乐捕鱼素材放入项目中 并启动tomca ...
- Unity 3(二):Unity在AOP方面的应用
本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): AOP简介: Interception using Unity示例 配置文件示例 一.AOP简介 AO ...