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 ...
随机推荐
- R kernel for Jupyter Notebook 支持r
1/2) Installing via supplied binary packages(default on Windows + Mac OS X) You can install all pack ...
- css--display属性中inline-block与inline的区别
inline-block 与 inline 的区别: inline-block 与inline 效果类似,但是inline-block是可以设定宽度和高度的!!而行内元素(也就是inline)是无法设 ...
- pymc
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- Hadoop生态圈-Hive快速入门篇之Hive环境搭建
Hadoop生态圈-Hive快速入门篇之Hive环境搭建 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据仓库(理论性知识大多摘自百度百科) 1>.什么是数据仓库 数据 ...
- HTML常用标签-<head>内常用标签
HTML常用标签-<head>内常用标签 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML是什么 1>.超文本标记语言(Hypertext Ma ...
- H5离线存储-manifest
起源 html5之前的网页,都是无连接,必须联网才能访问,这其实也是web的特色,这其实对于PC是时代问题并不大,但到了移动互联网时代,设备终端位置不再固定,依赖无线信号,网络的可靠性变得降低,比如坐 ...
- 【官方文档】Nginx负载均衡学习笔记(三) TCP和UDP负载平衡官方参考文档
本章介绍如何使用NGINX Plus和NGINX开放源代理和负载平衡TCP和UDP流量. 目录 介绍 先决条件 配置反向代理 配置TCP或UDP负载平衡被动健康监控 选择负载平衡方法 配置会话持久性 ...
- SELECT INTO 和 INSERT INTO SELECT比较
Insert是T-sql中常用语句,但我们在开发中经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用S ...
- Matlab debug
输入彩色,imwrite保存黑白图片,imwrite的维度错误. 程序如下,正常图像,少了一个维度imwrite,把图片展开,是一个二维的灰色图像(R=G=B),.如果限定了第二维,也是一个灰色图像. ...
- 【leetcode 简单】 第九十三题 二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...