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. jquery 格式化数字字符串(小数位)

    用于页面上格式化数字字符串,此代码为工作时所需,留作笔记,比较常用. /** * author: xg君 * 描述: 格式化数字字符串,格式化小数位 * obj为需要格式的对象(例如:input标签) ...

  2. JS动态更新微信浏览器中的title

    问题: 最近在做一个微信中分享的宣传页,分不同的场景,切换不同的场景时需要设置不同的title,实现的方案很简单,当用户切换场景的时候,修改document对象的title属性,可是在实际测试中,io ...

  3. bzoj千题计划144:bzoj1176: [Balkan2007]Mokia

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ分治 #include<cstdio> #include<iostre ...

  4. bzoj千题计划121:bzoj1033: [ZJOI2008]杀蚂蚁antbuster

    http://www.lydsy.com/JudgeOnline/problem.php?id=1033 经半个下午+一个晚上+半个晚上 的 昏天黑地调代码 最终成果: codevs.洛谷.tyvj上 ...

  5. 2017 清北济南考前刷题Day 2 afternoon

    期望得分:100+60+70=230 实际得分:0+60+0=60 T1 可以证明如果一对括号原本就匹配,那么这对括号在最优解中一定不会被分开 所以用栈记录下没有匹配的括号 最后栈中一定是 一堆右括号 ...

  6. 容斥 或者 单调栈 hihocoder #1476 : 矩形计数 和 G. Snake Rana 2017 ACM Arabella Collegiate Programming Contest

    先说一个简单的题目(题目大意自己看去,反正中文):hihocoder上的:http://hihocoder.com/problemset/problem/1476 然后因为这个n和m的矩阵范围是100 ...

  7. NGINX配置PHP解析

    <?php phpinfo(); ?> location ~ \.php$ { root html; fastcgi_pass ; fastcgi_index index.php; fas ...

  8. beta版1.1.2

    此次的beta版本做的修改重点在内部的算法上面. 因为之前所做的判断不重复的随机数方面采用的是String.valueof()的方式,即将int类型数字转换成string类型,比较string中是否出 ...

  9. 【转】 jquery easyui datagrid使用,分页、排序、查询

    $('#dg').datagrid({ url: "xxx.ashx",                    pagination: true,                p ...

  10. Caffe的loss layer(转)

    英文可查:地址 1.SoftmaxWithLoss 对一对多的分类任务计算多项逻辑斯蒂损失,并通过softmax传递预测值,来获得各类的概率分布.该层可以分解为SoftmaxLayer+Multino ...