Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 从给定字符串打印按字典顺序排序的子集_Algorithm_Sorting_Combinatorics - Fatal编程技术网

Algorithm 从给定字符串打印按字典顺序排序的子集

Algorithm 从给定字符串打印按字典顺序排序的子集,algorithm,sorting,combinatorics,Algorithm,Sorting,Combinatorics,对于ex-如果字符串是“abc”,则答案应该是 a ab abc ac b bc c(仅应出现一组字符中按词典编纂的最小组合) 我已经解决了这个问题,但是对于包含15个或更多字符的字符串,这需要花费很多时间。如何减少算法的运行时间? 这里,n是字符串的长度。 这是我的密码: int n = int.Parse(Console.ReadLine()); var str = Console.ReadLine(); string

对于ex-如果字符串是“abc”,则答案应该是 a ab abc ac b bc c(仅应出现一组字符中按词典编纂的最小组合) 我已经解决了这个问题,但是对于包含15个或更多字符的字符串,这需要花费很多时间。如何减少算法的运行时间? 这里,n是字符串的长度。 这是我的密码:

            int n = int.Parse(Console.ReadLine());
            var str = Console.ReadLine();
            string coll = string.Empty;
            coll = coll + " " + str[0];
            for (int j = 1; j < n; j++)
            {
                var items = coll.Split(' ');
                foreach (var item in items)
                {
                    coll = coll + " " + item+str[j];
                }
            }
            var tt = coll.Split(' ').OrderBy(a => a);
            foreach (var item in tt)
                if (!string.IsNullOrEmpty(item))
                    Console.WriteLine(item);
int n=int.Parse(Console.ReadLine());
var str=Console.ReadLine();
string coll=string.Empty;
coll=coll+“”+str[0];
对于(int j=1;ja);
foreach(tt中的var项目)
如果(!string.IsNullOrEmpty(项))
控制台写入线(项目);

对于长度为n的字符串,有2^n个可能的子集。如果每个子集都需要打印,那么就无法避开指数复杂性

但是
a ab abc ac b bc c
不是按长度排序的?你的代码是做什么的?为了更好地理解,编辑了这个问题。如果输入字符串为abcd,则答案应为ab abc abd ac acd ad b bc bcd bd c cd D此代码打印给定字符串的所有可能字母组合。如果有两个字符串具有相同的字符集,打印两条字符串按字典顺序排列的最小排列。您是指子集吗?你问题的标题有误导性。尽管有一些解决方案比其他方案更好,但看看这些建议会很有趣