在每个字符之间用点分割一个单词

时间:2016-06-24 05:25:23

标签: c#

我有一句话,例如:

qwerty

我想在每个字符之间添加一个点,但也要考虑所有可能的情况。

例如,输出应为:

qwerty
qwert.y
qwer.ty
qwer.t.y
qwe.rty
qwe.rt.y
qwe.r.ty
qwe.r.t.y
qw.erty
qw.ert.y
qw.er.ty
qw.er.t.y
qw.e.rty
qw.e.rt.y
qw.e.r.ty
qw.e.r.t.y
q.werty
q.wert.y
q.wer.ty
q.wer.t.y
q.we.rty
q.we.rt.y
q.we.r.ty
q.we.r.t.y
q.w.erty
q.w.ert.y
q.w.er.ty
q.w.er.t.y
q.w.e.rty
q.w.e.rt.y
q.w.e.r.ty
q.w.e.r.t.y

我到目前为止这段代码:

private void button12_Click(object sender, EventArgs e)
{
    int maxPossibilities = Convert.ToInt32(Math.Pow(2.0, txtInput.Text.Length));
    List<string> allPossibilities = new List<string>();

    for (int i = 0; i < maxPossibilities; i++)
    {
        string result = "";
        string added = Convert.ToString(i, 2).PadLeft(txtInput.Text.Length - 1);
        for (int j = 0; j < txtInput.Text.Length; j++)
        {
            result += txtInput.Text[j] + ((j < txtInput.Text.Length - 1) && (added[j].Equals('1')) ? "." : "");

        }
        allPossibilities.Add(result);
    }
}

我得到以下输出:

qwerty
qwert.y
qwer.ty
qwer.t.y
qwe.rty
qwe.rt.y
qwe.r.ty
qwe.r.t.y
qw.erty
qw.ert.y
qw.er.ty
qw.er.t.y
qw.e.rty
qw.e.rt.y
qw.e.r.ty
qw.e.r.t.y
q.werty
q.wert.y
q.wer.ty
q.wer.t.y
q.we.rty
q.we.rt.y
q.we.r.ty
q.we.r.t.y
q.w.erty
q.w.ert.y
q.w.er.ty
q.w.er.t.y
q.w.e.rty
q.w.e.rt.y
q.w.e.r.ty
q.w.e.r.t.y
q.werty
q.werty
q.wert.y
q.wert.y
q.wer.ty
q.wer.ty
q.wer.t.y
q.wer.t.y
q.we.rty
q.we.rty
q.we.rt.y
q.we.rt.y
q.we.r.ty
q.we.r.ty
q.we.r.t.y
q.we.r.t.y
q.w.erty
q.w.erty
q.w.ert.y
q.w.ert.y
q.w.er.ty
q.w.er.ty
q.w.er.t.y
q.w.er.t.y
q.w.e.rty
q.w.e.rty
q.w.e.rt.y
q.w.e.rt.y
q.w.e.r.ty
q.w.e.r.ty
q.w.e.r.t.y
q.w.e.r.t.y

输出中有一些重复,但不应该。任何帮助表示赞赏。

4 个答案:

答案 0 :(得分:2)

您的算法略有不同,会查看i的位以确定哪些位置应该得到点:

    string input = "qwerty";
    int maxPossibilities = Convert.ToInt32(Math.Pow(2.0, input.Length));
    List<string> allPossibilities = new List<string>();

    for(int i = 0; i < maxPossibilities; ++i) 
    {
        string result = "";
        for(int j = 0; j < input.Length; j++)
        {
            result += input[j];
            if((i & (1 << j)) != 0) { result += "."; }
        }

        allPossibilities.Add(result);
        System.Console.WriteLine(result);

    }

答案 1 :(得分:0)

如果您只是想将该项目添加到列表中(如果该项目已经存在),请尝试以下操作:

更改

allPossibilities.Add(result);

if (!allPossibilities.Contains(result)) { allPossibilities.Add(result); }

答案 2 :(得分:0)

试试这段代码,我知道它有点太多了,但它是结构化的,我使用位掩码在字符之间加点。

static void Main(string[] args)
{
    Do("qwerty");
}

public static void Do(string text)
{
    int i = 1;
    Text output = new Text();
    output.Add(new Dot(i));
    i = i * 2;
    foreach(char c in text)
    {
        output.Add(new textChar(c));
        output.Add(new Dot(i));
        i = i * 2;
    }

    int possibilites = output.Possibilites;
    for (i = 0; i < possibilites; i++)
    {
        Console.WriteLine(output.ToString(i));
    }

}

public class Text
{
    public List<character> characters = new List<character>();
    public void Add(character c)
    {
        characters.Add(c);
    }

    public int Possibilites
    {
        get
        {
            return Convert.ToInt32(Math.Pow(2, characters.Count(C => C is Dot)));
        }
    }

    public string ToString(int i)
    {
        string output = "";
        foreach (character c in characters)
        {
            if (c is textChar)
            {
                output += c.c;
            }
            else if (c is Dot)
            {
                if ((i & ((Dot)c).index) != 0)
                    output += c.c;
            }
        }

        return output;
    }

}
public class character
{
    public char c { set; get; }
}
public class textChar : character
{
    public textChar(char c)
    {
        this.c = c;
    }
}

public class Dot : character
{
    public int index;
    public Dot(int i)
    {
        index = i;
        c = '.';
    }
}

答案 3 :(得分:0)

另一个有效的解决方案。

这里点的位置作为布尔值在binarycount列表中保存为矩阵。它们定义了点的位置。

static void Main(string[] args)
{
    Console.WriteLine(String.Join(Environment.NewLine, printPossibilities("qwerty")));
}

private static List<string> printPossibilities(string str)
{
    List<string> allPossibilities = new List<string>();
    List<bool> binarycount = new List<bool>();
    for (int i = 0; i < str.Length - 1; i++) {
        binarycount.Add(false);
    }

    bool hasNext = true;
    while(hasNext)
    {
        string temp = str;
        for (int i = binarycount.Count - 1; i >= 0; i--)
        {
            if (binarycount[i])
                temp = temp.Insert(i+1, ".");
        }
        allPossibilities.Add(temp);
        hasNext = false;
        for (int i = binarycount.Count - 1; i >= 0; i--)
        {
            if (binarycount[i]) {
                binarycount[i] = false;
            } else {
                binarycount[i] = true;
                hasNext = true;
                break;
            }
        }
    }
    return allPossibilities;
}

输出:

qwerty
qwert.y
qwer.ty
qwer.t.y
qwe.rty
qwe.rt.y
qwe.r.ty
qwe.r.t.y
qw.erty
qw.ert.y
qw.er.ty
qw.er.t.y
qw.e.rty
qw.e.rt.y
qw.e.r.ty
qw.e.r.t.y
q.werty
q.wert.y
q.wer.ty
q.wer.t.y
q.we.rty
q.we.rt.y
q.we.r.ty
q.we.r.t.y
q.w.erty
q.w.ert.y
q.w.er.ty
q.w.er.t.y
q.w.e.rty
q.w.e.rt.y
q.w.e.r.ty
q.w.e.r.t.y