呵呵,标题显得有点复杂,希望大虾们不要看晕了。我最近碰到一个这样的问题,比如说:给我任意字符集{'a','b','c'},要求程序能输出的结果为:
{c,b,a,ba,ab,cb,bc,ca,ac,cba,bca,bac,cab,acb,abc},即所有可能由这些字符组成的字符串集合。我琢磨了好多天了也没弄出来。 希望有达人能帮我写一个实现上面问题的java 程序或算法,不甚感激!
{c,b,a,ba,ab,cb,bc,ca,ac,cba,bca,bac,cab,acb,abc},即所有可能由这些字符组成的字符串集合。我琢磨了好多天了也没弄出来。 希望有达人能帮我写一个实现上面问题的java 程序或算法,不甚感激!
private String initialStr; private int length; // 组合
private List comChar(String str) {
List list = new ArrayList();
int len = str.length();// str的字母个数len
int strlen = (1 << len) - 1;// str的字母可实现有组合数strlen
int tag[][] = new int[strlen][len];// 用一个strlen*len的二维数组来表示
int i, j, k, m; // 1至strlen的每个数字
String temp = ""; // 用一个字符串来获得每个数字对应的字母串
//System.out.println("总共可组成" + strlen + "种组合");
for (i = 1; i <= strlen; i++)// 得到每个数字的二进制表示,用tag[][]二维数组来接收
{
for (j = len, k = i; j > 0; j--) {
tag[i - 1][j - 1] = k % 2;
k = k / 2;
}
} for (i = 0; i < strlen; i++)// 依次打印出每个数字代表的组合
{
temp = "";
for (j = 0; j < len; j++) {
if (tag[i][j] == 0) {
continue;
} else {
temp = temp + str.charAt(j);
}
} list.add(temp);
//System.out.println(temp);
}
return list;
} // 排列
public void readInput(String initialStr) {
this.initialStr = initialStr;
//System.out.println("Please input a string:");
length = initialStr.length();
} public void permute() {
char[] initialChars = initialStr.toCharArray();
int[] mapNum = new int[length - 1];// 存放系数
char[] permutedChars = new char[length];// 存放排列的结果
boolean[] = new boolean[length];// true表示已经放了元素,false表示仍为空 for (int i = 0; i < mapNum.length; i++) {
mapNum[i] = 0;
} for (int i = 0; i < .length; i++) {
[i] = false;
} for (int i = 0; i < factorial(length); i++) {
int l = length - 2;
mapNum[l] = i / factorial(l + 1);
while (--l >= 0) {
mapNum[l] = i % factorial(l + 2) / factorial(l + 1);
}
for (int k = mapNum.length - 1; k >= 0; k--) {
int j = .length - 1;
int m = 0; while ((mapNum[k] + 1) != m)// 数出mapNum[k]+1个空格,最后一个来存放当前的intialChars[k+1]
{
if ([j] == false) {
m++;
} j--;
} j++;
permutedChars[j] = initialChars[k + 1];// 在第j个位置上放置元素
[j] = true;
} int j = length - 1;
while ([j] != false) {
j--;
} permutedChars[j] = initialChars[0]; System.out.println(new String(permutedChars)); if ((i + 1) % 5 == 0)
System.out.print(""); for (int q = 0; q < .length; q++) {
[q] = false;
} }
} private int factorial(int m) {
if ((m == 0) || (m == 1)) {
return 1;
} else {
return factorial(m - 1) * m;
}
} public static void main(String[] args) {
Comb p = new Comb();
//将字符串"abcd"进行组合
List list = p.comChar("abcd");
//将组合得到的结果进行排列
for (int i = 0; i < list.size(); i++) {
p.readInput((String)list.get(i));
if (p.length == 1) {
System.out.println(p.initialStr);
}
if (p.length == 2) {
System.out.println(p.initialStr);
System.out.println((new StringBuffer(p.initialStr)).reverse());
}
if (p.length >= 3) {
p.permute();
}
}
}}
{
public static void main(String[] args)
{
new Test().get(new char[]{'a', 'b', 'c'});
}
private char[] arr;
private char[] temp;
private boolean[] flag;
public void get(char[] s)
{
arr = s;
temp = new char[arr.length];
flag = new boolean[arr.length];
search(0);
}
private void search(int start)
{
if(start > 0)
{
for(int i = 0; i < start; i++) {
System.out.print(temp[i]);
}
System.out.print(" ");
if (start == arr.length)
return;
}
for(int j = 0; j < arr.length; j++)
{
if (!flag[j]) {
temp[start] = arr[j];
flag[j] = true;
search(start + 1);
flag[j] = false;
}
}
}
}
import java.util.List;public class Test {
public static void main(String args[]){
List al = new ArrayList();
List alResult = new ArrayList();
al.add("a");
al.add("b");
al.add("c");
alResult.addAll(al);
for(int i=0;i<al.size();i++){
for(int j=0;j<alResult.size();j++){
if(alResult.get(j).toString().indexOf(al.get(i).toString())==-1){
for(int k=0;k<alResult.get(j).toString().length();k++){
int len = alResult.get(j).toString().length();
String forward=alResult.get(j).toString().substring(0,k);
String back = alResult.get(j).toString().substring(k,len);
alResult.add(forward+al.get(i)+back);
}
}
}
}
System.out.println(alResult);
}
}
public class Test
{
public static void main(String[] args)
{
new Test().get(new char[]{'a', 'b', 'c'});
}
private char[] arr;
private char[] temp;
private boolean[] flag;public void get(char[] s)
{
arr = s;
temp = new char[arr.length];
flag = new boolean[arr.length];
search(0);//什么意思呀???
}private void search(int start)
{
if(start > 0)
{
for(int i = 0; i < start; i++) {
System.out.print(temp[i]);
}
System.out.print(" ");
if (start == arr.length)
return;
}for(int j = 0; j < arr.length; j++)
{
if (!flag[j]) {
temp[start] = arr[j];
flag[j] = true;//什么意思??
search(start + 1);
flag[j] = false;什么意思??
}
}
}}