:
내가 여기에 여기에 전체 코드를 게시하고 싶지만하지 않는 내가에 문제가하고있는 주요 부분이다
private static HashSet<String> s_Words = new HashSet<String>() {
"abacus",
//...
"flea",
"fleas",
//...
"sad",
"sea",
// ...
"zoom",
};
당신은 키이 그 안에 정렬 된 모든 문자 ("flea"
=>"aefl"
등 "sad"
=>"ads"
)와 초기 문자열 인 더 편리 집계 사전로 변환 할 수 있습니다.
"ale", "lea" => "ael" : ["ale", "lea"]
당신은 Linq를 통해 이러한 사전을 구현할 수 있습니다 :
private static Dictionary<String, String[]> s_Dict = s_Words
.Select(word => new {
Key = String.Concat(word.OrderBy(c => c)),
Value = word})
.GroupBy(item => item.Key, item => item.Value)
.ToDictionary(chunk => chunk.Key, chunk => chunk.ToArray());
두 개 이상의 단어가 같은 키를 가지고 있다면, 그들은 이 모음으로을 결합, 말, 배열해야한다 그런 다음 문자열을
String Input = "sasdfle"
당신이 할 필요가있다 종류 에 부여 93,692,858,을하고 각 문자를 포함하고 excuding 단지 256
(2 ** (length + 1) == 256
) 조합 확인 :
string source = String.Concat(Input.OrderBy(c => c));
// all combinations of the set with empty one excluded, see
// http://stackoverflow.com/questions/30081908/c-sharp-linq-combinatorics-all-combinations-of-a-set-without-the-empty-set/30082360#30082360
var result = Enumerable
.Range(1, (1 << source.Length) - 1)
.Select(index => string.Concat(source.Where((item, idx) => ((1 << idx) & index) != 0)))
.SelectMany(key => {
String[] words;
if (s_Dict.TryGetValue(key, out words))
return words;
else
return new String[0]; })
.Distinct() // some words can be built in many ways
.OrderBy(word => word);
//.ToArray(); // if you want to represent words as array
테스트
Console.Write(String.Join(Environment.NewLine, result));
가 반환됩니다
flea
fleas
sad
sea
는 당신이 우리를 보여줄 수있는 자신에 시도 무엇을 ..? 거기에 몇 가지 예가 있지만 다른 사람들이 현재 당신이 뭘하고 있는지 당신이 빠른 답변을 제공 대보고 싶습니다 확신합니다 .. 당신은 무엇을 시도 했습니까? – MethodMan
지금까지 시도한 내용에 대한 코드 게시를 고려하십시오. – Rahul
게시물을 업데이트했습니다. 나는 그것의 명확한 희망한다. –