C++ 生成无重复字符串的置换
我已经编写了一个通用程序来生成字符串的排列,但删除了重复的大小写。为此,我在用记忆的方法C++ 生成无重复字符串的置换,c++,algorithm,set,C++,Algorithm,Set,我已经编写了一个通用程序来生成字符串的排列,但删除了重复的大小写。为此,我在用记忆的方法 void permute(char *a,int i, int n,set<char*> s) { if(i==n) { if(s.find(a)==s.end()){ cout<<"no dublicate"<<endl; cout<<a<<endl;
void permute(char *a,int i, int n,set<char*> s)
{
if(i==n)
{
if(s.find(a)==s.end()){
cout<<"no dublicate"<<endl;
cout<<a<<endl;
s.insert(a)
}
}
else{
for(int j=i;j<n;j++)
{
swap(a[i],a[j]);
permute(a,i+1,n,s);
swap(a[i],a[j]);
}
}
}
int main()
{
char a[]="aba";
set <char*> s;
permute(a,0,3,s);
return 0;
}
void排列(字符*a,整数i,整数n,集合s)
{
如果(i==n)
{
如果(s.find(a)=s.end()){
cout首先,您通过值传递set s
参数,这将丢弃您的每次插入,因为它仅在s
的本地副本中完成。但是,即使您将其更改为通过引用传递,它也不起作用,因为每次插入相同的char*值,因此只会执行一次插入。为了使您的代码正常工作,我建议将函数的原型更改为
void permute(string a,int i, int n,set<string>& s)
void排列(字符串a、int i、int n、set&s)
这个很好用
更新:包含所述微小更改的源代码
void permute(string a,int i, int n,set<string>& s)
{
if(i==n)
{
if(s.find(a)==s.end()){
cout<<"no dublicate"<<endl;
cout<<a<<endl;
s.insert(a);
}
}
else{
for(int j=i;j<n;j++)
{
swap(a[i],a[j]);
permute(a,i+1,n,s);
swap(a[i],a[j]);
}
}
}
int main()
{
string a ="aba";
set <string> s;
permute(a,0,3,s);
return 0;
}
void排列(字符串a、int i、int n、set&s)
{
如果(i==n)
{
如果(s.find(a)=s.end()){
什么是“dublicates”?你是说你想要组合而不是排列?你试过std::next_排列吗?对于那些问他关于重复的意思的人,我想他是指(给定输入“aab”)他不希望“aab”返回两次(一次用于原始字符串,另一次用于第一个和第二个a
排列时)。上述原型不起作用。请使用codepad.org共享您的代码