- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想创建一个函数来按字典顺序从输入字符串中输出所有可能的排列。
我有以下代码:
void permutations_print(char *permutations, int index, int length) {
if (index == length) {
printf("\"%s\"\n", permutations);
}
for (int i = index; i < length; i++) {
rotate(permutations, i, index);
permutations_to_array(permutations, index + 1, length);
rotate_back(permutations, index, i);
}
}
void rotate(char to_swap[], int i, int j) {
char temp = to_swap[i];
for (int k = i; k > j; k--) {
to_swap[k] = to_swap[k - 1];
}
to_swap[j] = temp;
}
void rotate_back(char to_swap[], int i, int j) {
char tmp = to_swap[i];
for (int k = i; k < j; k++) {
to_swap[k] = to_swap[k + 1];
}
to_swap[j] = tmp;
}
输入字符串排列是permutations_print 排序。
对于只有唯一字符的排列,它可以正常工作,但我需要它也可以用于字符非唯一字符串,有什么想法可以调整/修改/工作吗?我必须使用递归并且不应该使用任何类型的排序并且我不应该将它存储在任何数组中,只是打印。我想打印所有内容,甚至是重复的。
最佳答案
警告:不是 C 程序员,所以我的代码肯定可以改进。
你可以用这样的方式迭代地做:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void swap(char* array, int i, int j);
void reverse(char* array, int left, int right);
bool nextPermutation(char* array, int n);
int compareCharacters(const void * a, const void * b);
void printPermutations(char *permutations, int length);
int main() {
char myArray[] = "hey";
printPermutations(myArray, 3);
return 0;
}
void printPermutations(char *array, int length) {
qsort(array, length, sizeof(char), compareCharacters);
*(array + length) = '\0';
do {
printf("%s\n", array);
} while (nextPermutation(array, length));
}
int compareCharacters(const void * a, const void * b) {
return (*(char*)a - *(char*)b);
}
bool nextPermutation(char* array, int n) {
if (n <= 1) {
return false;
}
// Find index, swapIndex1, of rightmost number that has a number greater than it
int swapIndex1;
for (swapIndex1 = n - 2; swapIndex1 >= 0; --swapIndex1) {
if (array[swapIndex1] < array[swapIndex1 + 1]) {
break;
}
}
if (swapIndex1 == -1) {
return false;
}
// Find index, swapIndex2, of smallest number to the right of that and greater than it
int swapIndex2 = swapIndex1 + 1;
int minToRight = array[swapIndex2];
for (int i = swapIndex2 + 1; i < n; ++i) {
if (array[i] <= minToRight && array[i] > array[swapIndex1]) {
minToRight = array[i];
swapIndex2 = i;
}
}
// Swap values at swapIndex1 and swapIndex2
swap(array, swapIndex1, swapIndex2);
// Reverse values from swapIndex1+1 to n-1
reverse(array, swapIndex1 + 1, n - 1);
return true;
}
void swap(char* array, int i, int j) {
char temp = array[i];
array[i] = array[j];
array[j] = temp;
}
void reverse(char* array, int left, int right) {
for (; left < right; ++left, --right) {
swap(array, left, right);
}
}
算法描述here .请参阅评论部分以获取示例/解释,或查看下面的转录:
Let's pretend we're finding the next largest permutation of digits from 1-9.
For example, suppose we have: 123479865
We want to find the smallest number greater than 123479865 that can be obtained by rearranging the digits of 123479865.
What this means is that we have to make at least one digit bigger than it currently is. If we had our choice, we would want to make the rightmost digit bigger since that will result in the smallest change. If we were to make a left number bigger, it would result in a bigger change in value.
e.g.: 123479865 => 123479866 is a much smaller change than 123479865 => 223479865.
Therefore, we want to find the farthest digit to the right that we can make bigger. In order to make a digit bigger, we have to find another number bigger than it within our sequence and then swap the two.
Note: we cannot swap a number (e.g., 5) with a number to its left (e.g., 6), because then we would be decreasing the value of a digit to our left, which would make our overall number smaller. For example, if we swapped 5 with 6, we would get 123479856, which is smaller than 123479865. Thus, we always swap with a number to our right.
So let's go through 123479865, starting with the rightmost digit.
There is nothing bigger than 5 to the right of 5, so we can't make 5 bigger.
Now let's consider 6. There is nothing bigger than 6 to the right of 6, so we can't make 6 bigger.
Now let's consider 8. There is nothing bigger than 8 to the right of 8, so we can't make 8 bigger.
Now let's consider 9. There is nothing bigger than 9 to the right of 9, so we can't make 9 bigger.
Now let's consider 7. There are a few numbers to the right of 7 that are bigger than 7, namely: 9 and 8. Therefore, we can make 7 bigger. We want to make it bigger by the least amount, so we should swap it with the smallest value that is bigger than 7. In other words, we should swap 7 with 8. That gives us: 123489765.
The number 123489765 is bigger than 123479865, but we can actually make it smaller while maintaining that it's bigger than 123479865. We can do this because we now have infinite freedom to change any of the following digits: 123489765 (anything to the right of 8). Those numbers can be as small as possible because the 8 to their left ensures that the new number is always bigger.
The best way to make the digits 9765 smaller is to sort them in increasing order, giving us 5679. Sorting works because the leftmost place values contribute the most to the overall value. Therefore, we want the leftmost digits to be the smallest digits.
That leaves us with 123485679, which is our answer.
Note: we don't actually have to sort the numbers to the right of 8. We can actually reverse their order because we know that the numbers from the right side to 8 are in increasing order (because earlier, we stopped the first time we got to a number that was smaller than its previous number).
关于c - 不具有唯一字符的字典顺序排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959719/
我只想国家和资本化的值(value)。 这是我的完整代码: cities = { 'rotterdam': { 'country': 'netherlands',
想更好地了解如何比较对象类型的键。 dicOverall.exists(dic2) 返回 False,而 dicOverall.exists(dic1) 返回 True。我不太确定 .Exists 如
我是编程和 python 的新手,我不知道如何解决这个问题。 my_dict = {'tiger': ['claws', 'sharp teeth', 'four legs', 'stripes'
这个问题已经有答案了: Accessing an object property with a dynamically-computed name (19 个回答) 已关闭 8 年前。 我引用了这篇文
希望有人能帮忙。我正在使用 Python,我希望能够执行以下操作。 我有一组对象(例如形状)和一系列作用于这些对象的命令。命令的格式为命令字符串,后跟可变数量的参数,可以是字符串或整数 例如形状“矩形
我在文件中保存了一本字典。我从 python 交互式 shell 将字典加载到内存中,我的系统监视器显示 python 进程消耗了 4GB。以下命令提供以下输出: size1 = sys.getsiz
如果我运行以下代码: import json foo = [ { "name": "Bob", "occupation": "", "stand
我尝试获取列名及其索引,并将结果保存为数据框或字典: df <- data.frame(a=rnorm(10), b=rnorm(10), c=rnorm(10)) 我该怎么做?谢谢。 column
我正在尝试获取输入,如果字典 logins 有一个与我的输入匹配的键,我想返回该键的值。 logins = { 'admin':'admin', 'turtle':'password1
在 Perl 世界中有一个很棒的东西叫做 CPAN .它是开源 Perl 库的大型存储。 我使用来自 CPAN 的模块,我已经发布了 several distributions myself . 我使
这个问题已经有答案了: Is there a Python dict without values? (3 个回答) 已关闭 3 年前。 我有一个问题,我想跟踪大量值。如果我从未遇到过该值,我将执行操
想知道这是否可能。 我们有一个第 3 方库,其中包含有关用户的识别信息... 与库的主要交互是通过一个以字符串为键的 HashTable,并返回该键的信息对象图。 问题是, key 显然是区分大小写的
我是 .NET 编程的新手。对不起,如果这个问题以前被问过。 我目前正在学习 F#。 Dictionary、Hashtable 和 Map 之间有什么区别?我应该什么时候使用? 我还有一个标题中没有提
我正在尝试使用SVM进行3类分类。为此,我正在SVM培训期间准备词汇表。但是,由于我在SVM预测期间获得随机结果,因此我怀疑我的词汇创建方法中存在一些问题。我创建词汇的代码如下: //Mat trai
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
假设我有一个以下形式的嵌套字典: {'geo': {'bgcolor': 'white','lakecolor': 'white','caxis': {'gridcolor': 'white', 'l
我有一个 java 应用程序,每秒启动和停止数亿个项目(从外部脚本调用)多次。 Input: String key Output: int value 此应用程序的目的是在从未永远改变的Map(约30
我正在尝试找出字典与集合和数组相比的相对优势和功能。 我发现了一篇很棒的文章here但找不到一个简单的表格来比较所有不同的功能。 有人知道吗? 最佳答案 请参阅下表,对集合和字典进行有用的比较。 (该
我想要一个字典,它可以为字典中没有的任何键返回一个指定的值,例如: var dict = new DictWithDefValues("not specified"); dict.Add("bob78
我是 python 新手,目前仍在学习如何处理列表和字典。 我有这两个功能 def food_database(item_name, size_serv, calorie_serv, prot
我是一名优秀的程序员,十分优秀!