带有输出长度限制和特殊参数的c++排列
C++ Permutation with a output length limit and special arguments
我是c++新手,我正在编写一个程序,该程序将生成字符串的所有排列列表,但是我需要将输出长度限制为5个字符(这很可能成为用户设置的可变数字)。我已经搜索了大约一个星期的东西像这样,我已经得到的最接近的是下面的代码。
Source.cpp:
#include <iostream>;
using namespace std;
void swap(char *fir, char *sec)
{
char temp = *fir;
*fir = *sec;
*sec = temp;
}
/* arr is the string, curr is the current index to start permutation from and size is sizeof the arr */
void permutation(char * arr, int curr, int size)
{
if(curr == size-1)
{
for(int a=0; a<size; a++)
cout << arr[a] << "";
cout << endl;
}
else
{
for(int i=curr; i<size; i++)
{
swap(&arr[curr], &arr[i]);
permutation(arr, curr+1, size);
swap(&arr[curr], &arr[i]);
}
}
}
int main()
{
string next;
char str[] = "abcdefghijklmnopqrstuvwxyz1234567890-";
permutation(str, 0, sizeof(str)-1);
cin.get();
cin.get();
}
这段代码可以工作,但是它不限制输出的长度。它将输出长度设置为给定字符串的长度。它似乎也不能解释输出中相同字母/数字的倍数(我不是100%确定)。
另外,我需要设置特殊的规则,比如连字符不能是输出的第一个字符或最后一个字符。
我尝试通过将sizeof(str)-1替换为5来修改上面的代码,但是它只会通过字符串中的前5个字符进行"循环",因此除了"e"之外的任何内容都不会被处理。
如果有人能帮助我,我将不胜感激。
<标题>编辑:感谢大家的大力帮助,我现在要张贴我的最终产品,以防其他人试图做同样的事情。
最终来源:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
void swap(char *fir, char *sec)
{
char temp = *fir;
*fir = *sec;
*sec = temp;
}
void permutation(char * arr, int size, char* result, int depth, int limit)
{
ofstream myfile ("permutation.txt", fstream::app);
if(depth == limit)
{
for(int a=0; a<limit; a++){
myfile << result[a] << "";
cout << result[a] << "";
}
myfile << "n";
cout << endl;
}
else
{
for(int i=0; i<size; i++)
{
result[depth] = arr[i];
permutation(arr, size, result, depth + 1, limit);
}
}
myfile.close();
}
int main()
{
ofstream myfile ("permutation.txt");
myfile << "";
myfile.close();
string answer;
char *rArray;
string startProcess = "N";
std::cout << "Welcome to permutation v1" << endl;
std::cout << "-------------------------" << endl;
std::cout << "Please enter how long the string should be: ";
std::getline (std::cin,answer);
int result = atoi(answer.c_str());
rArray = new char[result];
std::cout << "nnThank You!n" << endl;
std::cout << "Please wait, generating possible character array for length of " << result << "." << endl;
std::cout << "Would you like to proceed? Y = yes & N = no: ";
std::getline (std::cin,startProcess);
char str[] = "abcdefghijklmnopqrstuvwxyz1234567890";
if(startProcess == "Y")
{
permutation(str, sizeof(str)-1, rArray, 0, result);
}
else
{
std::cout << "nnOperation Terminated. No permutations being generated..." << endl;
}
cin.get();
return EXIT_SUCCESS;
}
标题>您需要限制递归的深度
给出字符串中字符的排列,每个字符只使用一次:
void permutation(char * arr, int currsize, intchar* sizeresult, int depth, int limit)
{
if(depth == limit)
{
for(int a=0; a<limit; a++)
cout << arr[a]result[a] << "";
cout << endl;
}
else
{
for(int i=curr;i=0; i<size; i++)
{
swap(&arr[curr],result[depth] &arr[i]);= arr[i];
permutation(arr, curr+1size, sizeresult, depth + 1, limit);
swap(&arr[curr], &arr[i]);
}
}
}
像这样调用
permutation(str, 0, sizeof(str)-1, result, 0, 5);
给出字符串中字符的排列,每个字符无限次使用:
void permutation(char * arr, int size, char* result, int depth, int limit)
{
if(depth == limit)
{
for(int a=0; a<limit; a++)
cout << result[a] << "";
cout << endl;
}
else
{
for(int i=0; i<size; i++)
{
result[depth] = arr[i];
permutation(arr, size, result, depth + 1, limit);
}
}
}
像这样调用
char result[5];
permutation(str, sizeof(str)-1, result, 0, sizeof(result));
这其实不是一项艰巨的工作。如果你能在函数中使用递归和循环,你就能解决这个问题。我建议使用标准库中的next_permutation
函数。
事实是时间。在3秒内,您可以处理仅8个字符的排列。条件将根据需要而定。假设在你的例子中,如果你需要省略开头或结尾的连字符,你可以删减。
我实现的伪代码:
char array[] = "abcdee";
char eachPerm[6];
bool usedmatrix[6][6];
Recur(int depth, int n)
{
// you can return from here if this is not the right path, suppose '-'
// in first or last place.
if(depth == n)
{
print;
}
else
{
int i;
for(i= 0 to array.length)
{
if(array[i] is not used before)
eachPerm[depth] = array[i];
recur(depth+1, n);
}
}
}
初始调用此函数recur(0, 5)
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用