Example #1
0
        public void Detect(string[] viruses)
        {
            var Reader = new ArgsReader(viruses);
            _Viruses = new List<string>(Reader.Viruses);

            // Zaczynamy od najkrotszych
            _Viruses = _Viruses.OrderBy(g => g.Length).ToList();

            CreateTree();
            ConstructPrefixGraph();
          
            bool HasCycle = false;
            List<Node> Topology = new List<Node>();

            // dla każdego wierzchołka rób
            foreach (Node LoopSafeNode in SafeNodesDictionary.Values)
            {
                // jeśli nie był jeszcze odwiedzony
                if (Topology.Contains(LoopSafeNode) == false)
                {
                    // dla każdej krawędzi z niego wychodzącej rób
                    Stack<Node> NodesToVisit = new Stack<Node>();
                    NodesToVisit.Push(LoopSafeNode);

                    while (NodesToVisit.Count > 0)
                    {
                        Node CurrentNode = NodesToVisit.Pop();

                        if (Topology.Contains(CurrentNode) == false)
                        {
                            Topology.Insert(0, CurrentNode);
                        }
                        else
                        {
                            HasCycle = true;
                        }

                        if (CurrentNode.Failure != null && Topology.Contains(CurrentNode.Failure) == false)
                        {
                            NodesToVisit.Push(CurrentNode.Failure);
                        }
                        foreach (var LoopChild in CurrentNode.Children)
                        {
                            if (LoopChild.IsDangerous == false && Topology.Contains(LoopChild) == false)
                            {
                                NodesToVisit.Push(LoopChild);
                            }
                        }
                    }

                }
            }
            

            if (HasCycle)
            {
                Result = Constants.PositiveResult;
            }
            else
            {
                Result = Constants.NegativeResult;
            }
        }
        public void Detect(string[] args)
        {
            ArgsReader Reader = new ArgsReader(args);
            Viruses = Reader.Viruses;

            _RemovalTotalTimeInMs = 0;
            _RemovedOccurencesMs = 0;
            Shorts = 0;
            Occurs = 0;

            Removal();

            // Shortener
            bool ReductionSuccess = false;
            bool RemovalSuccess = false;

            // Probuj skracac
            while (true)
            {
                RemovalSuccess = Removal();

                if (RemovalSuccess == false)
                {
                    ReductionSuccess = Reduction();
                }

                if (RemovalSuccess)
                {
                    Occurs++;
                }

                if (ReductionSuccess)
                {
                    Shorts++;
                }

                if (ReductionSuccess == false && RemovalSuccess == false)
                {
                    break;
                }
            }

            if (Viruses.Where(g => g.Length == 1).Count() > 0)
            {
                for (int i = 0; i < Viruses.Count; i++)
                {
                    int ZeroCount = 0;
                    int OneCount = 0;

                    foreach (Char c in Viruses[i])
                    {
                        if (c == '0')
                        {
                            ZeroCount++;
                        }
                        else if (c == '1')
                        {
                            OneCount++;
                        }
                    }

                    if (ZeroCount > 1 && OneCount == 0)
                    {
                        Viruses[i] = "0";
                    }

                    if (OneCount > 1 && ZeroCount == 0)
                    {
                        Viruses[i] = "1";
                    }
                }

            }

            if (Viruses.Where(Virus => Virus.Equals("1")).Count() > 0)
            {
                if (Viruses.Where(Virus => Virus.Equals("0")).Count() > 0)
                {
                    Result = Constants.NegativeResult;
                    return;
                }
            }

            Result = Constants.PositiveResult;
        }