//输出字符串所有组合
#include<iostream>
using namespace std;void zuhe(char *str,char *begin,int n)
{
if(str ==NULL || begin == NULL)//如果为空直接返回
return;
if(*begin == '\0')//每当bdgin为0时打印字符串
{
for(int i = 0;i < n;i++)
{
cout << str[i] << " ";
}
cout << endl;
}
for(char *pc = begin;*pc != '\0';pc++)
{
if(strchr(begin,*pc) == pc)//判断当前将要与begin交换的字符是不是第一次出现,如果是则交换,如果不是说明在begin的位置已经有过此字符,不在交换
{
char temp = *begin;
*begin = *pc;
*pc = temp;
zuhe(str,begin+1,n);//交换完,固定组begin,交换下一个位置
temp = *begin;//还回去
*begin = *pc;
*pc = temp;
}
}
}
int main()
{
char a[10];
int i = 0;
while(cin.get(a[i]) && a[i] != '\n')
i++;
a[i] = '\0';
zuhe(a,a,i);
return 0;
}
//输出字符串所有子串
#include<iostream>
#include<vector>
using namespace std;
void zichuan(char *str,vector<char>& result)
{
if(str ==NULL)
return;
if(*str =='\0')
{
for(vector<char>::iterator p = result.begin();p != result.end();p++)
{
cout << *p << " ";
}
cout << endl;
return;
}
result.push_back(*str);//递归实现
zichuan(str+1,result);
result.pop_back();
zichuan(str+1,result);
}
int main()
{
char a[10];
int i = 0;
while(cin.get(a[i]) && a[i] != '\n')
i++;
a[i] = '\0';
for(int j = 0;j < i;j++)//将字符串中重复的字符干掉后在排序
{
if(strchr(a,a[j]) == &a[j])
continue;
else
{
while(strchr(a,a[j]) != &a[j])
{
for(int k = j+1;k <= i;k++)
{
a[k-1] = a[k];
}
i--;
}
}
}
vector<char> b;
zichuan(a,b);
return 0;
}