简体   繁体   English

如何有效地生成2个字符的长词?

[英]How to generate 2-characters long words effectively?

I've been playing with idea to make a script to generate 2-characters words from a given set of characters in my language. 我一直在想制作一个脚本,以用我的语言从给定的一组字符生成2个字符的单词。 However, as I am not into re-inventing the wheel, do you know about such a script publicly available for C#? 但是,由于我不想重新发明轮子,您是否知道C#公开可用的这种脚本?

I'm not sure if I understood your question correctly, but this might help: 我不确定我是否正确理解了您的问题,但这可能会有所帮助:

List<string> GetWords(IEnumberable<char> characters) {
    char[] chars = characters.Distinct().ToArray();
    List<string> words = new List<string>(chars.Length*chars.Length);
    foreach (char i in chars)
       foreach (char j in chars)
          words.Add(i.ToString() + j.ToString());
    return words;
}

Are you talking about finding real two-character words that can be made from any combination of a list of characters? 您是在谈论寻找可以由任何字符列表组合而成的真正的两个字符的单词吗?

In which case, you need to write an algorithm that can work out all the possible combinations from the letters provided, and for each combination, try it (and the reverse) against an IDictionary that acts like a real dictionary of real two-letter words. 在这种情况下,您需要编写一种算法,可以从提供的字母中算出所有可能的组合,对于每种组合,请对IDictionary进行尝试(以及反向操作),该IDictionary的作用类似于真实的两个字母单词的真实词典。

Untested code: 未经测试的代码:

IDictionary<string, string> dictionary = GetRealTwoLetterWordDictionary();
char[] availableChars = new char[] { 'a', 's', 't' };
string[] combinations = GetAllCombinations(availableChars);
IList<string> results = new List<string>();

foreach (string combination in combinations)
{
    if (dictionary.ContainsKey(combination)))
    {
        results.Add(combination);
    }

    string reversed = combination.Reverse();

    if (dictionary.ContainsKey(reversed)))
    {
        results.Add(reversed);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM