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 (递归全排列+排序)的更多相关文章

  1. python非递归全排列

    刚刚开始学习python,按照廖雪峰的网站看的,当前看到了函数这一节.结合数组操作,写了个非递归的全排列生成.原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位 ...

  2. C#实现(递归和非递归)高速排序和简单排序等一系列排序算法

        本人由于近期工作用到了一些排序算法.就把几个简单的排序算法.想冒泡排序,选择排序,插入排序.奇偶排序和高速排序等整理了出来,代码用C#代码实现,而且通过了測试.希望能给大家提供參考.     ...

  3. 非递归全排列 python实现

    python algorithm 全排列(Permutation) 排列(英语:Permutation)是将相异物件或符号根据确定的顺序重排.每个顺序都称作一个排列.例如,从一到六的数字有720种排列 ...

  4. 牛客多校第十场 E Hilbert Sort 递归,排序

    题意: 给你一个方阵,再在方阵上给定一些点,按照希尔伯特曲线经过的先后顺序为这些点排序 题解: 定义好比较函数后直接调用排序算法即可. 希尔伯特曲线本来就是用于二维到一维的映射的,因此我们可以考虑对于 ...

  5. Codeforces 919D:Substring(拓扑排序+DP)

    D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...

  6. Codeforces 919D Substring 【拓扑排序】+【DP】

    <题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...

  7. sicily 1046. Plane Spotting(排序求topN)

    DescriptionCraig is fond of planes. Making photographs of planes forms a major part of his daily lif ...

  8. Codefroces 919D Substring(拓扑排序+DP)

    题目链接:http://codeforces.com/problemset/problem/919/D 题目大意:给你一张有向图,给你每个顶点上的字母和一些边,让你找出一条路径,路径上的相同字母数最多 ...

  9. 递归merge排序

    package sort; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scan ...

随机推荐

  1. Kubernetes Pod 健康检查

    参考文档: https://jimmysong.io/kubernetes-handbook/guide/configure-liveness-readiness-probes.html 一.Pod的 ...

  2. P1621 集合

    P1621 集合 题目描述 现在给你一些连续的整数,它们是从A到B的整数.一开始每个整数都属于各自的集合,然后你需要进行一下的操作: 每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于P的公共 ...

  3. python---基础知识回顾(十)进程和线程(py2中自定义线程池和py3中的线程池使用)

    一:自定义线程池的实现 前戏: 在进行自定义线程池前,先了解下Queue队列 队列中可以存放基础数据类型,也可以存放类,对象等特殊数据类型 from queue import Queue class ...

  4. bzoj千题计划158:bzoj2406: 矩阵(有源汇上下界可行流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2406 设矩阵C=A-B 最小化 C 一行或一列和的最大值 整体考虑一行或者一列的和 二分最大值 这样 ...

  5. Windows系统环境下Solr之Java实战(二)配置从MySQL数据库批量导入索引

    1.将D:\JavaWeb\Solr\solr-6.2.0\dist下面的solr-dataimporthandler-6.2.0.jar和solr-dataimporthandler-extras- ...

  6. Linux 下子进程与父进程的关系

    我们知道,Linux下父进程可以使用fork 函数创建子进程,但是当父进程先退出后,子进程会不会也退出呢? 通过下面这个小实验,我们能够很好的看出来: /******** basic.c ****** ...

  7. 简明Python教程 ~ 随书笔记

    本文是阅读<简明Python教程>所做的随书笔记,主要是记录一些自己不熟悉的用法,或者所看到的比较有意思的内容,本书英文版A Byte of Python, 中文译版 简明Python教程 ...

  8. vue_router 动态路由

    配置子路由: 路由的视图都需要使用view-router 子路由也可以嵌套路由使用: children来做嵌套如上图 使用location.页面name就可以做页面跳转 mounted:挂载,延迟跳转 ...

  9. vue_列表渲染

    vue列表渲染 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  10. javascript 中检测数据类型的方法

    typeof 检测数据类型 javascript 中检测数据类型有好几种,其中最简单的一种是 typeof 方式.typeof 方法返回的结果是一个字符串.typeof 的用法如下: typeof v ...