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[8];
string fullstr;
bool visited[8];

void recur(int cur) {
    if (cur == len) {
        // compelete a full string consist of len substrings
        permutation.insert(fullstr);
    } else {
        for (int i = 0; i < len; ++i) {
            if (!visited[i]) {
                int lastEnd = fullstr.size();

                // add another substring
                fullstr += substring[i];
                visited[i] = true;

                recur(cur + 1);

                // 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 = 0; i < len; ++i) {
            cin >> substring[i];
        }

        recur(0);

        // use the lexicographically smallest full string
        cout << *(permutation.begin()) << ‘\n‘;
    }
    return 0;
}
时间: 2024-10-29 19:10:08

sicily 1198. Substring (递归全排列+排序)的相关文章

sicily 1198 substring 组合问题

1198. Substring Constraints Time Limit: 1 secs, Memory Limit: 32 MB 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.

Sicily 1198 Substring

1198. Substring Constraints Time Limit: 1 secs, Memory Limit: 32 MB 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.

递归全排列(C#)

递归全排列(C#): static int[] xx=new int[4]; static void Main(string[] args) { printP(4, xx, 0); } static void printP(int n,int [] A,int cur) { int i, j; if (cur == n) { for (i = 0; i < n; i++) Console.Write(A[i]); Console.WriteLine(); } else for (i = 1; i

【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”},

Num 33 : 函数递归 [ 全排列 ]

数学上的全排列问题: 给定m个数,可以排列成n位数的所有情况: 例:3 个数 ( 1,2,3 ) 排列成两位数[ 含有重复数字 ]有: 11,12 ,13,21,22,23,31,32,33: 例:2个数( 1,2 ) 排列成三位数: 111, 112, 121, 122, 211, 212, 221, 222: 由上易找到规律:         对于 n 位 m 个要求的数 [ 不妨设为1,2,3的 2位数 ],我们只需从最高位开始,依次冠以第一个数( 11 );         之后保持高位

python非递归全排列

刚刚开始学习python,按照廖雪峰的网站看的,当前看到了函数这一节.结合数组操作,写了个非递归的全排列生成.原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位置插入,生成n+1个新的全排列.因为Python切割数组或者字符串,以及合并比较方便,所以,程序会节省很多代码. 1 def getArrayInsertCharToStr(STR,CHAR): 2 arr =[] 3 s_len = len(STR) 4 index =0 5 while inde

uva 10344 23 out of 5(递归+全排列)

这道题好像不是回溯就是简单的递归,全排列一下就ok啦,晚上估计不能好好刷题了 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int cmp(const void *c,const void *d) { return *(int *)c - *(int *)d ; } int flag; int a[5];

递归 冒泡排序 排序

//10以内把所有能被2整除的数想乘 //递归(定义:自己调用自己) function ceng(val){ if(val == 1){ console.log(val); return 1; } if(val%2 !== 0){ return ceng(val-1); } return val * ceng(val-1); /*var n = 1; for(var i=1;i<=val;i++){ if(i % 2 == 0){ n = n * i; } } return n;*/ } var

php 无限级分类 递归+sort排序 和 非递归

1 先总结非递归 数据表: id name pid path 1 php 0 0 2 mysql 0 0 3 linux 0 0 4 php-基本语法 1 0-1 5 linux-磁盘分区 3 0-3 1 <?php 2 3 $navArr = [ 4 [ 5 'name'=>'php', 6 'id'=>1, 7 'pid'=>0, 8 'path'=>'0', 9 'sort'=>'2', 10 11 ], 12 [ 13 'name'=>'php-基础',