只需两次更改即可查找Start String和End String之间的字符串

时间:2013-10-18 17:17:02

标签: c# asp.net string list character-encoding

假设我有一个带字符串的txt文件,我有开始单词和结束单词。我需要找到一个字符不同的起始单词和结束单词之间的所有单词。 如何查找一个字符不同的字符串?我是否需要将从文件中读取的字符串分解为字符并与我的开始和结束字符串进行比较

3 个答案:

答案 0 :(得分:0)

  

我需要找到起始单词和结束单词之间的所有单词   不同的一个字;所以根据我的结果给出的例子   是:AAAA,ABZZ和AAZZ。

任务看起来很简单,但我对你的例子感到有些困惑...... 我接下来你会得到List:

AAAA ABAA ABZA ABZZ AAZZ

以下是样本:

 private int Differs(string Word1, string Word2) //calculates how much letters differs in 2 words
        {
         //Here should be some checks to prevent index out of range error during cycle
         int result=0;
         for(int i=0;i<Word1.Length;i++) 
         if(Word1[i]!=Word2[i]) result++;
         return result;
        }

 public List<string> ArrangeWords(string StartWord, string EndWord, List<string> Words)
        {
            List<string> ResultWords = new List<string>();
            string CurrentWord = StartWord;
            Words.Remove(StartWord); //removing start and end words from list
            Words.Remove(EndWord);
            ResultWords.Add(StartWord);
            while (Words.Count > 0) //Searching next words and moving them to result list until Source List becomes empty
            {
                List<string> neubourwords = Words.FindAll(x => Differs(CurrentWord, x) == 1);
                if (neubourwords.Count > 0)
                {
                    CurrentWord = neubourwords[0];
                    ResultWords.Add(CurrentWord);
                    Words.Remove(CurrentWord);
                }
                else return null; //wrong source sequence!
            }
            ResultWords.Add(EndWord);
            return ResultWords;
        }


 private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string StartWord = "AAAA";
            string EndWord = "AAZZ";
            List<string> Words = new List<string>() { "ABAA", "AAAA", "ABZA", "ABZZ", "AAZZ" }; //List, loaded from file
            List<string> ArrangedWords=ArrangeWords(StartWord,EndWord,Words);
}

如果单词的复杂性增加,任务将与使用图表(http://veda.cs.uiuc.edu/courses/fa09/cs466/lectures/Lecture9.pdf)的DNA测序alghorythm非常类似。

答案 1 :(得分:0)

enter image description here

Theres需要一些递归函数来找到最短路径。

答案 2 :(得分:0)

这称为字符串之间的距离。 看看这个:( Levenshtein距离)

http://www.dotnetperls.com/levenshtein http://en.wikipedia.org/wiki/Levenshtein_distance