求字符的组合的算法,原题目如下:Problem: Use your most hands-on programming language to implement a function that prints all possible combinations of the characters in a string. These combinations range in length from one to the length of the string. Two combinations that differ only in ordering of the characters ARE the same combination. In other words, “12” and “31” are different combinations from the input string “123”, but “21 is the same as “12”.For example, if we use “wxyz” as parameter for Combination(“wxyz”) the function will print outw
x
y
z
wx
wy
wz
xy
xz
yz
wxy
wxz
wyz
xyz
wxyz
x
y
z
wx
wy
wz
xy
xz
yz
wxy
wxz
wyz
xyz
wxyz
解决方案 »
- SqlCommandBuilder为什么不按主键更新?
- winform里怎么从窗体1传值给窗体2里的textbox并显示出来?
- visual C# 2005中像QQ那样抽屉式的控件在那里
- 为什么刷新的时候会重复执行按钮的脚本?
- c#下怎么实现串品通讯? 请指点!
- 保存 JPG 文件时,如何设定压缩率?
- 关于C#调用C库文件出现的错误,新手真诚请求指点
- Convert.ToInt16的反操作是什么?
- 正则表达式的问题
- 请问怎么取随机数
- 用ADO.net操作数据库(插入,更改等)代码好象没有问题,但我在打开SQL SERVER2000企业管理器时怎么数据表不随着更新,
- 自定义控件的属性值无法保存, 请看一下代码,谢谢
void getchar(char * p)
{
int m = strlen(p);
int x = m;
while(x--)
{
int n = m;
int * b = new int [m];
while(n--)
{
if(n<x)
{
b[n] = 1;
}
else
{
b[n] = 0;
}
}
for(i = 0,n = 1;n<m;n++,i++)
{
if(b[n] && !b[n+1])
{
b[n] = 0;
b[n+1] = 1;
for(int j = 0;j< m;j++)
{
cout<<p[b[j]]);
}
cout<<endl; n = 0;
i = 0;
}
else if(i == m)
{
break;
}
}
}
}
按字符拆分wxyz
分出4个 w,x,y,z
拿出第一个w 与后面与w,x,y,z进行组合(不与与自身有相同字符的组合,也就是w)组出 wx wy wz
拿出x 与wx wy wz比较 找出拥有其字符所有组合的字符(w x,不与其组合),得出xy,xz
拿出y与wx wy wz xy,xz 比较…… yz然后拿出wx 与其以外的字符组合 wxy wxz
wy,方法同上与 wxy wxz 比较 组成wyz
wz 与wxy wxz wyz 发现拥有其组合包含了所有字符 不再组合
……
----
写好才看到这个-_-
用Pascal写了一下,代码如下,只做过简单的测试:program CharacterCombine;{$APPTYPE CONSOLE}var
Chars : array[1..108] of char;
Occurs : array[1..108] of integer;
MaxCharCount : integer;procedure AddChar(chr : Char);
var
i : integer;
exists : boolean;
begin
exists := false;
for i := 1 to MaxCharCount do
begin
if Chars[i] = chr then
begin
Inc(Occurs[i]);
exists := true;
break;
end;
end; if not exists then
begin
Inc(MaxCharCount);
Chars[MaxCharCount] := chr;
Occurs[MaxCharCount] := 1;
end;
end;procedure MakeOutput(charCount, currentPos, currentCharUsed : integer; currentOutputString : string);
var
i : integer;
outputLen : integer;
begin
outputLen := Length(currentOutputString); if outputLen = charCount then
begin
WriteLn(currentOutputString);
exit;
end; if currentCharUsed < Occurs[currentPos] then
begin
MakeOutput(charCount, currentPos, currentCharUsed + 1, currentOutputString + Chars[currentPos]);
end; for i := currentPos + 1 to MaxCharCount do
begin
MakeOutput(charCount, i, 1, currentOutputString + Chars[i]);
end;
end;var
inputString : string;
inputLen : integer;
i : integer;
begin
ReadLn(inputString);
while inputString <> ''do
begin
inputLen := Length(inputString); MaxCharCount := 0;
for i := 1 to inputLen do
begin
AddChar(inputString[i]);
end; for i := 1 to inputLen do
begin
MakeOutput(i, 1, 0, '');
end; ReadLn(inputString);
end;
end.
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string star=Console.ReadLine();
// SortAll(star);
// Console.ReadLine();
CombAll(star);
SortAll(star);
Console.ReadLine();
}
#region 全排列
static void SortAll(string star)
{
string intStar="|";
for(int i=0;i<star.Length ;i++)
{
intStar+=i.ToString()+"|";
}
Sort(star,intStar,"|");
} static void Sort(string star,string intStar,string StrIn)
{
string result="|";
int i=star.Length ;
if(StrIn.Length ==intStar.Length )
{
string[] temp=StrIn.Split('|');
string StrOut="";
for(int k=1;k<star.Length+1 ;k++)
{
int t=Convert.ToInt32(temp[k]);
StrOut+=star.Substring(t,1);
}
Console.WriteLine(StrOut);
}
else
{
#region 开始循环取值
for(int j=0;j<i;j++)
{
string mid="";
string[] temp=intStar.Split('|');
mid=temp[j+1];
//mid=intStar.Substring(j,1);
if(StrIn.IndexOf("|"+mid+"|")>=0)
{
}
else
{
bool flag=true;
//判断后面的字符是否用过
//bool flag0=true; //mid对应的字符
string mid0=star.Substring(j,1);
//已经构成排列的数字字符串数组。
string[] ok=StrIn.Split ('|');
//依次取后面的字符
for(int k=j+1;k<i;k++)
{
if(StrIn.IndexOf("|"+k.ToString()+"|")>=0) //用过
{
}
else //没有用过
{
if(mid0==star.Substring(k,1)) //有相同的
{
flag=false;
break;
}
else
{
flag=true;
}
}
}
if(flag)
{
result=StrIn+mid+"|";
Sort(star,intStar,result);
}
}
}
#endregion
}
}
#endregion
#region 全组合
static void CombAll(string star)
{
string intStar="|";
for(int i=0;i<star.Length ;i++)
{
intStar+=i.ToString()+"|";
}
for(int i=1;i<star.Length +1;i++)
{
Combination(star,"|","",i);
}
}
/// <summary>
/// 给定字符,给定长度(长度小于给定字符串的长度),列出所有组合
/// 即给定的字符不能重复使用
/// </summary>
/// <param name="star">给定的初始字符串</param>
/// <param name="INstartar">给定的初始字符串</param>
/// <param name="l">组合后的字符串长度</param>
///
static void Combination(string star,string IntIn,string StrIn,int l)
{
if(StrIn.Length >=l)
{
if(StrIn.Length ==l)
{
Console.WriteLine(StrIn);
}
}
else
{
#region 开始循环取值
for(int j=0;j<star.Length ;j++)
{
string mid="";
bool flag=true;
mid=star.Substring(j,1);
if(IntIn.IndexOf("|"+j.ToString()+"|")>=0)
{
}
else
{
for(int k=j+1;k<star.Length ;k++) //一次取后面的字符
{
if(IntIn.IndexOf("|"+k.ToString()+"|")>=0) //用过
{
}
else
{
if(mid==star.Substring(k,1))
{
flag=false;
break;
}
else
{
flag=true;
}
}
} //一次取后面的字符
if(flag)
{
bool lastflag=false;
if(IntIn.Length >1)
{
int Intpre0=IntIn.LastIndexOf("|");
string tempIntIn=IntIn.Remove(Intpre0,1);
int Intpre1=tempIntIn.LastIndexOf("|");
string Intpre=IntIn.Substring(Intpre1+1,Intpre0-Intpre1-1); int intPre=Convert.ToInt32(Intpre);
if(intPre>j)
{
lastflag=true;
}
else
{
if(intPre<j)
{
lastflag=false;
}
else
{
if(mid==StrIn.Substring(StrIn.Length-1,1))
{
lastflag=true;
}
else
{
lastflag=false;
}
}
}
}
else
{
lastflag=true;
} if(lastflag)
{
string IntInTemp=IntIn+j.ToString()+"|";
//IntIn+=j.ToString()+"|";
string StrInTemp=StrIn+mid;
//StrIn+=mid;
Combination(star,IntInTemp,StrInTemp,l);
}
}
}
}
#endregion
}
}
#endregion
/// <summary>
/// 给定字符,给定长度(长度小于给定字符串的长度),列出所有排列
/// 即给定的字符不能重复使用
/// </summary>
/// <param name="star"></param>
/// <param name="IntIn"></param>
/// <param name="StrIn"></param>
/// <param name="l"></param>
///
static void Sort(string star,string IntIn,string StrIn,int l)
{
if(StrIn.Length >=l)
{
if(StrIn.Length ==l)
{
Console.WriteLine(StrIn);
}
}
else
{
#region 开始循环取值
for(int j=0;j<star.Length ;j++)
{
string mid="";
bool flag=true;
mid=star.Substring(j,1);
if(IntIn.IndexOf("|"+j.ToString()+"|")>=0)
{
}
else
{
for(int k=j+1;k<star.Length ;k++) //一次取后面的字符
{
if(IntIn.IndexOf("|"+k.ToString()+"|")>=0) //用过
{
}
else
{
if(mid==star.Substring(k,1))
{
flag=false;
break;
}
else
{
flag=true;
}
}
} //一次取后面的字符
if(flag)
{
/*
bool lastflag=false;
if(IntIn.Length >1)
{
int Intpre0=IntIn.LastIndexOf("|");
string tempIntIn=IntIn.Remove(Intpre0,1);
int Intpre1=tempIntIn.LastIndexOf("|");
string Intpre=IntIn.Substring(Intpre1+1,Intpre0-Intpre1-1); int intPre=Convert.ToInt32(Intpre);
if(intPre>j)
{
lastflag=true;
}
else
{
if(intPre<j)
{
lastflag=false;
}
else
{
if(mid==StrIn.Substring(StrIn.Length-1,1))
{
lastflag=true;
}
else
{
lastflag=false;
}
}
}
}
else
{
lastflag=true;
}
*/
//if(lastflag)
//{
string IntInTemp=IntIn+j.ToString()+"|";
//IntIn+=j.ToString()+"|";
string StrInTemp=StrIn+mid;
//StrIn+=mid;
Combination(star,IntInTemp,StrInTemp,l);
//}
}
}
}
#endregion
}
}
}
}
inputString : string;
inputLen : integer;
i : integer;
从这一段代码开始相当于C中的main函数
{
...
}
_______________________________________________________________________对你来说可能是玩,对我来说是饭碗的问题啊!!!!!!!!!!!!
http://www.bxmy.org/article.html
你的方法有问题,我帮你调了一下
得出来的结果:xxwx
xwxx
xwxw
xwwx其它没看
{
int m = strlen(p);
int x = m;
while(x--)
{
int n = m;
int * b = new int [m];
while(n--)
{
if(n<x)
{
b[n] = 1;
}
else
{
b[n] = 0;
}
}
for(i = 0,n = 1;n<m;n++,i++)
{
if(b[n] && !b[n+1])
{
b[n] = 0;
b[n+1] = 1;
for(int j = 0;j< m;j++)
{
if(b[j])
{
cout<<p[b[j]];
}
}
cout<<endl; n = 0;
i = 0;
}
else if(i == m)
{
break;
}
}
}
}
我上面的算法和文章中的方法有些接近,呵呵,
可惜,现在不能调试,郁闷中。。
using System;
using System.Collections;namespace AllCombinations
{
class GenerateWords
{
public static void Main()
{
ArrayList res = Generate_Permutations("abcd");
Console.WriteLine( "Count = " + res.Count);
for(int i=0;i<res.Count;i++)
{
Console.WriteLine(res[i]);
}
}
public static ArrayList Generate_Permutations(string word)
{
ArrayList result = new ArrayList();
result.Add(word[word.Length-1]);
if(word.Length <= 1)
return result;
for(int i=word.Length -2 ;i>=0;i--)
{
int count = result.Count;
for(int j=0;j<count;j++)
{
result.Add(word[i].ToString() + result[j].ToString());
}
result.Add(word[i].ToString());
}
return result;
}
}
}// The results is :
Count = 15
d
cd
c
bd
bcd
bc
b
ad
acd
ac
abd
abcd
abc
ab
a
表示敬意!
虽然用的c#,但代码非常精炼,执行效率也不差,过程也很巧妙!
function mm(n: integer): integer;
var
i: integer;
begin
result := 1;
for i := 1 to n do result := result * 2; // 求2^n的函数
end;
procedure zh(n: integer);
var
i, j, n2, n1: integer;
s: array of string;
begin
n1 := mm(n); //求2的n次方
SetLength(s, n1 + 1);
mmo1.Lines.Clear;
for i := 1 to n do
begin
n2 := mm(i - 1);
for j := 1 to n1 do
begin
if ((j div n2) mod 2) = 1 then s[j] := s[j] + IntToStr(i);
end;
end;
for I := 1 to n1 do
mmo1.Lines.Add(s[i]);
end;
var
i: Integer;
begin
zh(4);
end;