给出一个函数来输出一个字符串的所有排列。

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

#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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航