给出一个函数来输出一个字符串的所有排列。
2005-09-06 20:09
447 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <vector>
using namespace std;
char *p = "abcde";
vector<int> st;
/**找index以后的、没有在st中的数。
*如果不存在则返回-1
*/
int nextVal(int index)
{
int len = strlen(p);
vector<int>::iterator it;
for(int i = (index + 1); i < len; i++ )
{
for(it = st.begin(); it < st.end(); it++)
{
if(p[*(it)] == p[i]) break;
}
if(it >= st.end())
{
return i;
}
}
return -1;
}
/**
*z在屏幕上输出vector st的内容
*/
void printVector()
{
vector<int>::iterator it;
for(it = st.begin(); it < st.end(); it++)
{
cout <<*it << " ";
}
cout << endl;
}
/**
*输出p字符串中的字符的排列
*/
void arrange(char *p)
{
assert(p != NULL);
int len = strlen(p);
int val;
int ret;
int temp;
int temp2;
int count = 0;
while(true)
{
temp = (len - st.size());
for(int i = 0; i < temp; i++)
{
temp2 = nextVal(-1);
st.push_back(temp2);
}//end of for
printVector();
cout << "the count is" << ++count << endl;
while(st.size() != 0)
{
val = *(st.end()-1);
st.pop_back();
ret = nextVal(val);
if(ret != -1)
{
st.push_back(ret);
break;
}
else
{
}
}//end of while
if(st.size() ==0)
{
break;
}
}//end of while
}//end of arrange
int main()
{
arrange(p);
return 1;
}
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <vector>
using namespace std;
char *p = "abcde";
vector<int> st;
/**找index以后的、没有在st中的数。
*如果不存在则返回-1
*/
int nextVal(int index)
{
int len = strlen(p);
vector<int>::iterator it;
for(int i = (index + 1); i < len; i++ )
{
for(it = st.begin(); it < st.end(); it++)
{
if(p[*(it)] == p[i]) break;
}
if(it >= st.end())
{
return i;
}
}
return -1;
}
/**
*z在屏幕上输出vector st的内容
*/
void printVector()
{
vector<int>::iterator it;
for(it = st.begin(); it < st.end(); it++)
{
cout <<*it << " ";
}
cout << endl;
}
/**
*输出p字符串中的字符的排列
*/
void arrange(char *p)
{
assert(p != NULL);
int len = strlen(p);
int val;
int ret;
int temp;
int temp2;
int count = 0;
while(true)
{
temp = (len - st.size());
for(int i = 0; i < temp; i++)
{
temp2 = nextVal(-1);
st.push_back(temp2);
}//end of for
printVector();
cout << "the count is" << ++count << endl;
while(st.size() != 0)
{
val = *(st.end()-1);
st.pop_back();
ret = nextVal(val);
if(ret != -1)
{
st.push_back(ret);
break;
}
else
{
}
}//end of while
if(st.size() ==0)
{
break;
}
}//end of while
}//end of arrange
int main()
{
arrange(p);
return 1;
}
相关文章推荐
- 算法40(字符串的排列,给出一个函数来输出一个字符串的所有排列)
- 给出一个函数来输出一个字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列。
- 微软等数据结构+算法面试100题(2)--给出一个函数来输出一个字符串的所有排列
- 3、给出一个函数输出字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 笔试题:给出一个函数来输出字符串的所有排列
- 70 给出一个函数来输出一个字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 写一个函数输出一个字符串中的所有排列
- 给出一个函数来输出一个字符串的所有排列
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- 写正确函数需要注意的地方:输入一个字符串,打印出该字符串中字符的所有排列
- 编写函数fun(char *s),其功能是:把字符串中所有字符前移一个位置,串中的第1个字符移到最后。 例如,若输入字符串为:ABC123xyz ,则应输出字符串: BC123xyzA 。
- 使用内建的isinstance函数将一个list中所有的字符串变成小写,非字符串直接输出
- C#实现输入一个字符串输出该字符串字符的所有排列方式
- 题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。
- 已知一个字符串,输出它包含字符的所有排列(permutations)
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。