如果string str="abc";要求输出ab,ac,bc,abc
如果string str="abcd";要求输出ab,ac,ad,bc,bd,cd,abc,abd,acd,bcd,abcd,
如果string str="abcde";要求输出ab,ac,ad,ae,bc,bd,be,cd,ce,de,abc,abd,abe,acd,ace,ade,bcd,bce,bde,cde,abcd,abce,abde,acde,bcde
字符串的长度不确定,但大于等于2

解决方案 »

  1.   

    p(n,n) 高中数学知识,组合问题
      

  2.   

    没有要求 ab ac 可以为ac,ab
    不过ab不能写成ba
      

  3.   

    字符串 就是按顺序 abcdefg 这样下去的 还是会打乱的?
      

  4.   

    字符串是随便的
    cba 也可以的 
    cba 输出 cb,ca,ba,cba
      

  5.   

    字符会重复出现吗? 比如 abcda
      

  6.   

    如果string str="abcdef";输出什么?
      

  7.   

    土方法:效率极低请使用 XXOO("abcde")
    public static List<string> XXOO(string data)
    {
        List<string> result = new List<string>();
        foreach (string str in _XXOO(data))
        {
            if (str.Length > 1)
            {
                result.Add(str);
            }
        }
        return result;
    }public static List<string> _XXOO(string data)
    {
        int length = data.Length;
        if (length > 0)
        {
            List<string> result = new List<string>();
            for (int i = 0; i < length; i++)
            {
                result.Add(data[i].ToString());
            }        for (int i = 0; i < length - 1; i++)
            {
                string swap = data[i].ToString();
                List<string> lswap = _XXOO(data.Substring(i + 1));
                foreach (string str in lswap)
                {
                    result.Add(swap + str);
                }
            }
            return result;
        }
        else
        {
            return new List<string>();
        }
    }
      

  8.   

    ab,ac,ad,ae,af,bc,bd,be,bf,cd,ce,cf,de,df,ef,abc,abd,abe,abf,acd,ace,acf,ade,adf,aef,
    abcd,abce,…………
      

  9.   

    可以参考http://topic.csdn.net/t/20031126/00/2495045.html
      

  10.   

    求所有子集的算法阿。
    网上找了个
    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            /**//// <summary>
            /// 函数:给定一个集合,求出其所有子集合
            /// </summary>
              private static List<string> printList(string[] arr, int num)
            {
                if (num < 0)
                {
                    List<string> reto = new List<string>();
                    reto.Add("");
                    return reto;
                }
                else
                {
                    List<string> ret = printList(arr, num - 1);
                    List<string> addRet = new List<string>();
                    ret.ForEach(delegate(string x) { addRet.Add(x +" "+ arr[num]); });
                    ret.AddRange(addRet);
                    return ret;
                }
            }
            static void Main(string[] args)
            {
                //演示
                string[] arr ={ "a", "b", "c"};
                List<string> ret = printList(arr, 2);
                ret.ForEach(delegate(string x) { Console.WriteLine(x); });
                Console.Read();
            }
        }
    }
      

  11.   

    private List<string> getall(string s)
            {
                List<string> ss = new List<string>();
                ss.Add(s);
                int t = s.Length;            List<string> tempss = new List<string>();
                List<string> tempss2 = new List<string>();
                tempss2.AddRange(ss);            while (t > 2)
                {
                    tempss.Clear();
                    tempss.AddRange(tempss2);
                    tempss2.Clear();
                    foreach (string subs in tempss)
                    {
                        for (int i = 0; i < subs.Length; i++)
                        {
                            string ts = subs.Remove(i, 1);
                            if(!tempss2.Contains(ts))
                            tempss2.Add(subs.Remove(i, 1));
                        }
                    }
                    t--;
                    ss.AddRange(tempss2);
                }
                return ss;
            }
      

  12.   

    采用递归函数
    [a] --> {}{a}
    [ab] --> [a] [b]
    abc --> [ab] [c] 
      

  13.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication6
    {
        class Program
        {
            static List<String> Result = new List<String>();        static void Main(string[] args)
            {
                string str = "abcde";            for (int i = 0; i < str.Length; i++)
                {
                    String S = str.Substring(i, str.Length - i);                GetIt(new List<Char>(S.ToCharArray()), String.Empty, 0);
                }            Result.Sort();
                foreach (String S in Result)
                    Console.Write(S + " ");            Console.Read();
            }        static void GetIt(List<Char> S, String R, int Pos)
            {
                for (int i = 0; i < S.Count; i++)
                {
                    Char C = S[i];                if (R.IndexOf(C) > -1)
                        continue;                String X = R + C;
                    Char[] XCs = X.ToCharArray();
                    Array.Sort(XCs);                X = String.Empty;
                    foreach (Char XC in XCs)
                        X += XC.ToString();                if (Result.IndexOf(X) == -1 && X.Length > 1)
                        Result.Add(X);                List<Char> T = new List<Char>(S);
                    T.RemoveAt(0);                GetIt(T, R + C.ToString(), Pos);
                }
            }
        }
    }
      

  14.   

    这个pos不需要using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication6
    {
        class Program
        {
            static List<String> Result = new List<String>();        static void Main(string[] args)
            {
                string str = "abcde";            for (int i = 0; i < str.Length; i++)
                {
                    String S = str.Substring(i, str.Length - i);                GetIt(new List<Char>(S.ToCharArray()), String.Empty);
                }            Result.Sort();
                foreach (String S in Result)
                    Console.Write(S + " ");            Console.Read();
            }        static void GetIt(List<Char> S, String R)
            {
                for (int i = 0; i < S.Count; i++)
                {
                    Char C = S[i];                if (R.IndexOf(C) > -1)
                        continue;                String X = R + C;
                    Char[] XCs = X.ToCharArray();
                    Array.Sort(XCs);                X = String.Empty;
                    foreach (Char XC in XCs)
                        X += XC.ToString();                if (Result.IndexOf(X) == -1 && X.Length > 1)
                        Result.Add(X);                List<Char> T = new List<Char>(S);
                    T.RemoveAt(0);                GetIt(T, R + C.ToString());
                }
            }
        }
    }
      

  15.   


    private void button1_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = string.Empty;
                UpdateText(textBox2.Text);
            }        private IList _text = new ArrayList();        private void TextAdd(string str)
            {
                for (int i = 0; i < _text.Count; i++)
                {
                    if (_text[i].ToString() == str)
                    {
                        return;
                    }
                }
                _text.Add(str);
                this.textBox1.Text += str + "\r\n";
            }        private void UpdateText(string str)
            {
                TextAdd(str);
                if (str.Length < 3)
                {
                    return;
                }            char[] list = str.ToCharArray();
                for (int i = 0; i < str.Length; i++)
                {
                    string s = str.Remove(str.Length - 1 - i, 1);
                    UpdateText(s);
                }
            }
    实现了,但是性能不好,需要判断重复。
      

  16.   

    public class Test {

    //cnt 表示子第一次打印要求的字符串的长度
    public void  getSubString(String parentString,int cnt){
    for(int i=0;i<parentString.length()-cnt+1;i++){
    for(int j=i+1;j<parentString.length()-cnt+1;j++){
    System.out.println(""+parentString.substring(i,i+1)+parentString.substring(j,j+cnt-1));
    }
    }
    if(cnt++ == parentString.length()){
    System.out.println(parentString);
    }else{
    getSubString(parentString,cnt);
    }
    }
    public static void main(String[] args){
    Test test = new Test();
    test.getSubString("abcdefghi",2);
    }
    }
      

  17.   

    http://topic.csdn.net/t/20031126/00/2495045.html
    排列组合问题,上面的网址有介绍算法的,可以参考
      

  18.   

    古老的问题了google :“全排列算法”
      

  19.   

    楼主拿我代码去试试
    测试下效率
    今天突发奇想想出来的:        private static void ZhiMaKaiMen(string sIn)
            {
                int iLen = sIn.Length;
                if (iLen <= 2)
                    Console.WriteLine(sIn);
                char[] cALL = sIn.ToCharArray();            Hashtable htRank = new Hashtable();
                htRank.Add(Convert.ToInt64(0),0);
                for (int r = 0; r < iLen; r++)
                {
                    htRank.Add(Convert.ToInt64(Math.Pow(2, r)), r);
                }            string sTmp = string.Empty;
                BitArray ba = null;
                for (int i = 0; i < Math.Pow(2, iLen); i++)
                {
                    if (!htRank.Contains(Convert.ToInt64(i)))
                    {
                        sTmp = string.Empty;
                        ba = new BitArray(BitConverter.GetBytes(i));
                        for (int j = 0; j < ba.Length; j++)
                        {
                            if (ba[j])
                                sTmp += cALL[j].ToString();
                        }
                        Console.WriteLine(sTmp);
                    }
                }
            }调用 ZhiMaKaiMen("abcd");
      

  20.   

    班门弄斧一下
    新手,用数组写一个
    MAX定义最大长度
    试过,还可以吧。#include"stdio.h"
    #include"string.h"
    #define MAX 10
    void main()
    {
        char a[MAX+1];
        int i=0,j=i+1,k=j-1,l,s,b=0;
        scanf("%s",a);
        s=strlen(a);
        while(i<s-1)
        {
            if(k++>=s-1)
            {
                if(++j!=s)
                {
                    k=j;
                }
                else
                {
                    i++;
                    j=i+1;
                    k=j;
                }    
            }
            for(l=i;(l<j)&&(i!=s-1);l++)
            {
                printf("%c",a[l]);
                b=1;
            }
            if(b==1)
            printf("%c,",a[k]);
            b=0;
        }
    }
        
        
      

  21.   

    static string[] m_Data = { "A", "B", "C", "D", "E" };
            static void Main(string[] args)
            {
                Dictionary<string, int> dic = new Dictionary<string, int>();
                for (int i = 0; i < m_Data.Length; i++)
                {
                    
                    dic.Add(m_Data[i], i);
                }
                GetString(dic);
                Console.ReadLine();
            }
            static void GetString(Dictionary<string, int> dd)
            {
                Dictionary<string, int> dic = new Dictionary<string, int>();
                foreach (KeyValuePair<string, int> kv in dd)
                {
                    for (int i = kv.Value + 1; i < m_Data.Length; i++)
                    {
                        //Console.WriteLine(kv.Key + " " + kv.Value + " " + i);
                        Console.WriteLine(kv.Key + m_Data[i]);
                        dic.Add(kv.Key + m_Data[i], i);
                    }
                }
                //Console.WriteLine(dic.Count);
                if (dic.Count > 0) GetString(dic);
            }
    ================================
    运行结果: 





    AB 
    AC 
    AD 
    AE 
    BC 
    BD 
    BE 
    CD 
    CE 
    DE 
    ABC 
    ABD 
    ABE 
    ACD 
    ACE 
    ADE 
    BCD 
    BCE 
    BDE 
    CDE 
    ABCD 
    ABCE 
    ABDE 
    ACDE 
    BCDE 
    ABCDE
      

  22.   

    我这层楼的。顺便说一下,只是针对含有的元素(字符)不相同的。若输入出现相同的如aba则认为有三个不同的字符。没有检测功能。并且输出的方式和其他人的不同
    但是如果假设字符全部相同,效率还可以。改了一下MAX的值,当有25个输入的时候,测试了一下。用时0.054945。
      

  23.   

    LZ会写小九九么?
    把str拆开
    类似小九九的方式输出
    string类型的值相加的结果 例如a+b 得到的是ab 1+2得到的是12
    有点思路没有??
      

  24.   

    开始以为是标题党,上午看到标题就没进去看,晚上无意中点到了
    这个问题我以前也想过,一直考虑的是取字符的下标来确定,没想出来。通过字符串截取很容易解决上面的问题!
                string str = "ABCDEFG";
                for (int num = 1; num < str.Length; num++)
                {
                    for (int i = 0; i < str.Length; i++)
                    {
                        for (int j = i + 1; j < str.Length; j++)
                        {
                            int count = num - 1 + j;
                            if (count < str.Length)
                                Console.WriteLine(str.Substring(i, num) + str.Substring(count, 1));
                        }
                    }
                }
                /*
                ------输出结果------------
                AB
                AC
                AD
                AE
                AF
                AG
                BC
                BD
                BE
                BF
                BG
                CD
                CE
                CF
                CG
                DE
                DF
                DG
                EF
                EG
                FG
                ABC
                ABD
                ABE
                ABF
                ABG
                BCD
                BCE
                BCF
                BCG
                CDE
                CDF
                CDG
                DEF
                DEG
                EFG
                ABCD
                ABCE
                ABCF
                ABCG
                BCDE
                BCDF
                BCDG
                CDEF
                CDEG
                DEFG
                ABCDE
                ABCDF
                ABCDG
                BCDEF
                BCDEG
                CDEFG
                ABCDEF
                ABCDEG
                BCDEFG
                ABCDEFG
                */
      

  25.   


                string[] str = { "", "a", "b", "c", "d", "e","f",g" };            int L = str.Length - 1;
                string T = "";
                for (int i = 1; i < L; i++)
                {
                    T = str[i];
                    for (int j = i; j < L; j++)
                    {
                        if (j > i) T += str[j];
                        for (int k = j; k < L; k++)
                            Console.WriteLine(T + str[k + 1]);
                    }
                }
      

  26.   


    string[] str = { "", "a", "b", "c", "d", "e","f","g" };掉引号了 
      

  27.   


    private StringBuilder  RepComponent(string str,StringBuilder sRet)
            {
                if (str.Length <= 1)
                    return sRet;
                string s = str.Substring(0, 1);
                for (int i = 1; i < str.Length; i++)
                {
                    sRet.Append(s + str[i] + ",");
                }
                if (str.Length > 2)
                {
                    sRet.Append(str + ",");
                }
                sRet = RepComponent(str.Substring(1), sRet);
                return sRet;
            }呵呵,楼主接贴速度真快。把我的思路贴上供参考
      

  28.   

    有人可能看不懂63的代码:我帮忙翻译成人类能理解的形式~!^_^!~
     int iLen = sIn.Length;//这个取要组合字符串的长度
                if (iLen <= 2)//如果小于2直接输出来
                    Console.WriteLine(sIn);//输出
                char[] cALL = sIn.ToCharArray();//把它用一个个字符装进去一个数组离去            Hashtable htRank = new Hashtable();//创建哈希表
                htRank.Add(Convert.ToInt64(0), 0);//添加一个0,0第一个是键,第2个是值
                for (int r = 0; r < iLen; r++)//循环数组
                {
                    htRank.Add(Convert.ToInt64(Math.Pow(2, r)), r);//添加键为1,2,4,8...
                    //值为0,1,2,3
                }            string sTmp = string.Empty;//先设为空
                BitArray ba = null;//先设为空值
                for (int i = 0; i < Math.Pow(2, iLen); i++)//又是循环1,2,4,8...
                {
                    if (!htRank.Contains(Convert.ToInt64(i)))//将不是哈希表中值为1,2,4,8的
                                                                    //提取出来即0,3,5,6,7,9...
                    {
                        sTmp = string.Empty;//还是设为空
                        ba = new BitArray(BitConverter.GetBytes(i));//这个就有讲究了^_^
                                    //再次我简单举例一下:如果是6的话,6/2=3 为0,3/2=1 为1
                                    //那么就为  011 记住是011哦!下面就有用了!                    for (int j = 0; j < ba.Length; j++)//ba.length一般为32(如果是INT类
                                                                   //型的话)BITARRAY()里面的值不是
                                                                 //true,就是FALSE
                        {
                            if (ba[j])//如果值为6的话(举例)是011 J=0 FALSE,J=1TURE call[1]
                                     //是多少?当然是2, j=2 true call[2] 是多少? 当然是 3哈哈,就这样!下面的STMP就来累加,2,3当然就是输出23了一次类推~哈哈,
                                sTmp += cALL[j].ToString();//那么这边取值是多少
                        }
                        Console.WriteLine(sTmp);
                    }
                }