发现的话所有的排列在一个句子 [英] finding all permutation of words in a sentence
本文介绍了发现的话所有的排列在一个句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能得到的话都排列在sentence.can你给一个示例C#code是什么?
例如:如果一句话就是C#不是Java
输出应该是,1)C#不是Java 2)C#的Java不是3)不Java的C#4)Java的C#不是5)不是Java C#6)不是C#的java等。
how can i get all permutation of words in a sentence.can you give a sample c# code for that? eg: if the sentence is "C# not java", the output should be, 1)c# not java 2)c# java not 3)java not c# 4)java c# not 5)not java c# 6)not c# java etc.
推荐答案
尝试,如果这对你的作品。
Try if this works for you.
public static List<string> PermuteWords(string s)
{
string[] ss = s.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries);
bool[] used = new bool[ss.Length];
string res = "";
List<string> list = new List<string>();
permute(ss, used, res, 0, list);
return list;
}
private static void permute(string[] ss, bool[] used, string res, int level, List<string> list)
{
if (level == ss.Length && res != "")
{
list.Add(res);
return;
}
for (int i = 0; i < ss.Length; i++)
{
if (used[i]) continue;
used[i] = true;
permute(ss, used, res + " " + ss[i], level + 1, list);
used[i] = false;
}
}
这篇关于发现的话所有的排列在一个句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文