2013-02-21 87 views
0

目前有一个小项目我正在摆弄,这是一个倒计时类型的游戏(电视节目)。 目前,该程序允许用户选择一个元音辅音或到9个字母的限制,然后将它们要求输入他们能想到用这些由9个字母的最长的单词。如果搜索字符串数组包含数组,如果字符的

我有作为,我通过使用用户输入的字符串匹配尝试的结果来检查,如果他们进入这个词是一个有效的搜索词的字典大文本文件。我的问题是,我想,然后搜索我的字典由九个字母的最长的单词,但我不能似乎找到一个方法来实现它。

到目前为止,我已经尝试将每个单词放入一个数组中,并通过每个元素进行搜索以检查它是否包含字母,但是如果可以由9个字母组成的最长单词是8个字母字。有任何想法吗? 目前我有这个(这是表单提交按钮下,不提供代码或提抱歉它是一个Windows窗体应用程序):

StreamReader textFile = new StreamReader("C:/Eclipse/Personal Projects/Local_Projects/Projects/CountDown/WindowsFormsApplication1/wordlist.txt"); 
int counter1 = 0; 
String letterlist = (txtLetter1.Text + txtLetter2.Text + txtLetter3.Text + txtLetter4.Text + txtLetter5.Text + txtLetter6.Text + txtLetter7.Text + txtLetter8.Text + txtLetter9.Text); // stores the letters into a string 
char[] letters = letterlist.ToCharArray(); // reads the letters into a char array 
string[] line = File.ReadAllLines("C:/Eclipse/Personal Projects/Local_Projects/Projects/CountDown/WindowsFormsApplication1/wordlist.txt"); // reads every line in the word file into a string array (there is a new word on everyline, and theres 144k words, i assume this will be a big performance hit but i've never done anything like this before so im not sure ?) 

line.Any(x => line.Contains(x)); // just playing with linq, i've no idea what im doing though as i've never used before 
for (int i = 0; i < line.Length; i++)// a loop that loops for every word in the string array 
// if (line.Contains(letters)) //checks if a word contains the letters in the char array(this is where it gets hazy if i went this way, i'd planned on only using words witha letter length > 4, adding any words found to another text file and either finding the longest word then in this text file or keeping a running longest word i.e. while looping i find a word with 7 letters, this is now the longest word, i then go to the next word and it has 8 of our letters, i now set the longest word to this) 

counter1++; 
if (counter1 > 4) 

txtLongest.Text + =行+ Environment.NewLine;

麦克的代码:using System.Collections.Generic

using System; 

;使用System.Linq的 ;

类节目

static void Main(string[] args) { 
    var letters = args[0]; 

    var wordList = new List<string> { "abcbca", "bca", "def" }; // dictionary 

    var results = from string word in wordList // makes every word in dictionary into a seperate string 
        where IsValidAnswer(word, letters) // calls isvalid method 
        orderby word.Length descending // sorts the word with most letters to top 
        select word; // selects that word 

    foreach (var result in results) { 
     Console.WriteLine(result); // outputs the word 
    } 
} 

private static bool IsValidAnswer(string word, string letters) { 
    foreach (var letter in word) { 
     if (letters.IndexOf(letter) == -1) { // checks if theres letters in the word 
      return false; 
     } 

     letters = letters.Remove(letters.IndexOf(letter), 1); 
    } 

    return true; 
} 

}

+0

对于Adrian的观点,它有助于发布您编写的代码,因此我们知道从哪里开始。 – 2013-02-21 16:53:04

+0

如果我理解正确,这听起来像是一个NP难题。有(可能)没有这样做的简单方法。看看[coin-problem](http://en.wikipedia.org/wiki/Coin_problem) – Automatico 2013-02-21 16:56:18

+1

你可以重复使用字符吗?如果你选择a,b,c,d,e,f,g,h,i - 你能拼写'egg'吗?还是你应该选择'g'两次? – amit 2013-02-21 17:00:17

回答

1

这是我在几分钟内敲打的一个答案,它应该做你想做的。正如其他人所说,这个问题很复杂,所以算法会变得很慢。 LINQ查询评估字典中的每个字符串,检查所提供的字母是否可用于生产说一句话。

using System; 
using System.Collections.Generic; 
using System.Linq; 

class Program 
{ 
    static void Main(string[] args) { 
     var letters = args[0]; 

     var wordList = new List<string> { "abcbca", "bca", "def" }; 

     var results = from string word in wordList 
         where IsValidAnswer(word, letters) 
         orderby word.Length descending 
         select word; 

     foreach (var result in results) { 
      Console.WriteLine(result);  
     } 
    } 

    private static bool IsValidAnswer(string word, string letters) { 
     foreach (var letter in word) { 
      if (letters.IndexOf(letter) == -1) { 
       return false; 
      } 

      letters = letters.Remove(letters.IndexOf(letter), 1); 
     } 

     return true; 
    } 
} 
+0

感谢您的帮助@mike,工作就像一个魅力:) – 2013-03-01 09:58:42

-1

您是否尝试实施这样的事情?看到你的代码尝试过会很高兴。

string[] strArray = {"ABCDEFG", "HIJKLMNOP"}; 
string findThisString = "JKL"; 
int strNumber; 
int strIndex = 0; 
for (strNumber = 0; strNumber < strArray.Length; strNumber++) 
{ 
    strIndex = strArray[strNumber].IndexOf(findThisString); 
    if (strIndex >= 0) 
     break; 
} 
System.Console.WriteLine("String number: {0}\nString index: {1}", 
    strNumber, strIndex); 
+0

它是如何解决问题的?如果它在那里,它将如何找到“LKJ”这个词?它如何找到可以使用字符构造的**最长的单词**。请解释你的答案。 – amit 2013-02-21 17:02:14

0

那么你卡在哪里?从缓慢的蛮力方法开始,找到所有包含所有字符的单词。然后按长度排序以获得最长的单词。如果你不想返回一个字短于所寻求的字符数(我的猜测是只有一个问题,如果有重复的字符???),然后添加一个测试,消除这种情况。

-1

这必须做的工作:

private static void Main() 
{ 
    char[] picked_char = {'r', 'a', 'j'}; 
    string[] dictionary = new[] {"rajan", "rajm", "rajnujaman", "rahim", "ranjan"}; 
    var words = dictionary.Where(word => picked_char.All(word.Contains)).OrderByDescending(word => word.Length); 


    foreach (string needed_words in words) 
    { 
     Console.WriteLine(needed_words); 
    } 
} 

输出:


rajnujaman
ranjan
rajan
rajm

+0

此代码不能完成这项工作。作为一个测试用例,将pick_char设置为'a','a','j','n','n','r','x','y','z'并查看您返回的单词。 – 2013-02-21 20:38:34

0

我对此有了更多的想法。我认为有效地做到这一点的方法是对字典进行预处理,按字母顺序排列每个字母中的字母,并按字母顺序排列列表中的字词(您可能必须使用某种多图结构来存储原始字词和排序的词)。

一旦你这样做了,你可以更有效地找到可以从你的字母池中生成的单词。如果其他人没有打败我,我会回来补充一个算法来做到这一点。

0

第1步:构建一个trie结构,每个字母按字母排序。例如:将EACH排序为ACEH,将其作为A→C→E→H→(EACH,ACHE,..)存储在trie中(ACHE是EACH的一个字母)。

步骤2:对输入字母进行排序,找到与字典句子中对应的最长单词。

相关问题