计算字符串中的箭头

str*_*ing 1 .net c# arrays algorithm data-structures

我正在尝试编写代码来计算字符串中的箭头.我成功运行它,它的工作原理.

箭头就像这>>--> or <--<<两个都必须考虑到,我们也必须考虑箭头的共享部分,例如,如果我有<--<<--<<将计数2箭头.

我解决问题的代码是这样的:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringsArrows
{
    class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader reader = File.OpenText("C:\\Users\\Mohit\\Desktop\\PolmStudio Tasks\\StringsArrows\\StringsArrows\\File.txt"))
                 while (!reader.EndOfStream)
                 {
                     List<string> list = null;
                     string line = reader.ReadLine();
                     if (null != line)
                     {
                         list = new List<string>();
                         string[] digits = line.Split(new char[] {'\n' }, StringSplitOptions.RemoveEmptyEntries);
                         int counter = 0;
                         foreach (string word in digits)
                         {
                             for (int i = 0; i < word.Count();i++)
                             {
                                 if (i + 4 < word.Length)
                                 {
                                     if (word[i] == '<')
                                     {
                                         if (word[i + 1] == '-')
                                         {
                                             if (word[i + 2] == '-')
                                             {
                                                 if (word[i + 3] == '<')
                                                 {
                                                     if (word[i + 4] == '<')
                                                     {
                                                         counter++;
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                     if (word[i] == '>')
                                     {
                                         if (word[i + 1] == '>')
                                         {
                                             if (word[i + 2] == '-')
                                             {
                                                 if (word[i + 3] == '-')
                                                 {
                                                     if (word[i + 4] == '>')
                                                     {
                                                         counter++;
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         Console.WriteLine(" Num. Of arrows are :"+counter);
                     }
                 }
            Console.ReadKey();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从文件输入是这样的:

<--<<--<<
<<>>--><--<<--<<>>>--><
<-->>
Run Code Online (Sandbox Code Playgroud)

它的输出是这样的:

Num. Of arrows are :2
Num. Of arrows are :4
Num. Of arrows are :0
Run Code Online (Sandbox Code Playgroud)

我想要的是:有人可以知道是否有任何可能更优化的方式呢?

var*_*bas 6

我个人非常喜欢基于循环的解决方案(尽管你应该改进你的算法以减少大的缩进;通过函数/递归).但在这种特定情况下,有更简单有效的解决方案; 例如:依赖Split,这允许容易地确定某些子串重复的次数.

示例代码:

string input = "<<>>--><--<<--<<>>>--><";

string[] temp = input.Split(new string[] { ">>-->", "<--<<" }, StringSplitOptions.None);
int totArrows = temp.Length - 1;
temp = input.Split(new string[] { ">>-->>-->", "<--<<--<<" }, StringSplitOptions.None);

totArrows = totArrows + temp.Length - 1; //4
Run Code Online (Sandbox Code Playgroud)

UPDATE

通过评论证明,提议的方法可能无法在特定条件下提供正确的答案.另外,正如评论所解释的那样,我的意图并非始终构建一个完全解决所有OP问题的即用型代码; 但是以不同的方式面对问题并设置足够坚实的起点(实际上是最终的解决方案).

在任何情况下,为了完整性(通过评论奖励兴趣)并显示所提议Split方法的多种应用,在这些方面下面,我包括一个完全满足(似乎是)OP要求的不同算法.

string input2 = "<<>>--><--<<--<<>>>--><";

string[] temp2 = input2.Split(new string[] { "--" }, StringSplitOptions.None);
int totArrows2 = 0;

for(int i = 0; i < temp2.Length - 1; i++)
{
    string prevBit = temp2[i].Trim();
    string curBit = temp2[i + 1].Trim();

    if (prevBit.Length > 0 && curBit.Length > 0)
    {
        if (prevBit.Substring(prevBit.Length - 1, 1) == "<")
        {
            if (curBit.Length >= 2 && curBit.Substring(0, 2) == "<<") totArrows2 = totArrows2 + 1;
        }
        else if (prevBit.Length >= 2 && prevBit.Substring(prevBit.Length - 2, 2) == ">>")
        {
            if (curBit.Substring(0, 1) == ">") totArrows2 = totArrows2 + 1;
        }
    }
}

//totArrows2 = 4
Run Code Online (Sandbox Code Playgroud)