在本节中,我们将看到如何生成任意长度的所有可能字符串,这将采用每个字符的组合来生成字符串。例如,如果字符串是ABC,则它将生成 – {A,B,C,AB,BA,BC,CB,CA,AC,ABC,ACB,BAC,BCA,CAB,CBA}
让我们看一个例子来理解。
算法
printAllString(str)
Begin n := length of the string str count is 2^n – 1 for each number 0 to count, do sub_str := empty string for j in range 0 to n, do if jth bit of the counter is set, then concatenate jth character of str with sub_str end if done repeat: print sub_string until next permutation of sub_string is not completed doneEnd
登录后复制
示例
#include #include #include using namespace std;void printAllString(string str) { int n = str.size(); unsigned int count = pow(2, n); for (int counter = 1; counter输出
ABABBACACCABCCBABCACBBACBCACABCBADADDABDDBABDADBBADBDADABDBACDDCACDADCCADCDADACDCABCDBDCCBDCDBDBCDCBABCDABDCACBDACDBADBCADCBBACDBADCBCADBCDABDACBDCACABDCADBCBADCBDACDABCDBADABCDACBDBACDBCADCABDCBA登录后复制
以上就是给定一个字符串,将其组成的所有可能长度的字符串都列出来的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2585798.html