示例#1
0
            List <Utils.structResult> updateIndex(Utils.structWork work, int k, List <Utils.structResult> indexes)
            {
                List <Utils.structResult> newList = indexes;
                int j = 0;

                // for each char in pivot, look for a match in test string
                while (j < work.test.Length)
                {
                    Utils.structResult trial = checkChar(work.pivot, k, work.test, j);
                    if (trial.indTest != -1)
                    {
                        // found a match, store it into indexes
                        // eliminate duplicates
                        bool exist = false;
                        foreach (Utils.structResult y in newList)
                        {
                            if (y.cmpObj(trial))
                            {
                                exist = true;
                                break;
                            }
                        }
                        if (!exist)
                        {
                            trial.index = newList.Count;
                            newList.Add(trial);
                        }
                        j = trial.indTest + 1;
                    }
                    else
                    {
                        break;
                    }
                }
                return(newList);
            }
示例#2
0
            public bool findCommon()
            {
                X = args[2];
                Y = args[3];
                X = checker.checkString(checker.checkLength(N, X, mylog), X);
                Y = checker.checkString(checker.checkLength(M, Y, mylog), Y);

                strData = string.Format("TEST={4} actual     : {0} {1} {2} {3}\n", N, M, X, Y, ind + 1);
                mylog.Log(strData);
                // Environment.Exit(0);

                // look for the first char matched and added it to result
                // indexes is a list of structures for all matched chars
                List <Utils.structResult> indexes = new List <Utils.structResult>();

                // work is the working class - pivot string (the shortest) an test string
                Utils.structWork work = new Utils.structWork(X, Y);

                // loop on shortest string and build indexes
                for (int k = 0; k < work.pivot.Length; k++)
                {
                    indexes = updateIndex(work, k, indexes);
                }                  // for loop
                if (level >= 1)
                {
                    mylog.Log("indexes");
                    foreach (Utils.structResult y in indexes)
                    {
                        y.printResult("", mylog);
                    }
                }
                // results is a list of results : matched substring and its position in both strings
                List <Utils.finalResult> results = lookForStrings(indexes);

                // no common string
                if (results.Count == 0)
                {
                    strData = string.Format("The longest ({0} chars) common substring are:", 0);
                    mylog.Log(strData);
                    return(false);
                }
                // check which string is the longest
                int maxlength = 0;
                List <Utils.finalResult> max = new List <Utils.finalResult>();

                foreach (Utils.finalResult p in results)
                {
                    if (level >= 1)
                    {
                        p.printResult("final", mylog);
                    }
                    if (p.match.Length >= maxlength)
                    {
                        maxlength = p.match.Length;
                        if (maxlength > 0)
                        {
                            max.Add(p);
                        }
                    }
                }
                strData = string.Format("The longest ({0} chars) common substring are:", maxlength);
                mylog.Log(strData);

                // print all the strings with the length equal to the longest one
                foreach (Utils.finalResult p in max)
                {
                    if (p.match.Length == maxlength)
                    {
                        p.printResult(mylog);
                    }
                }
                if (level >= 3)
                {
                    strData = string.Format("Exit: {0}", System.Reflection.MethodBase.GetCurrentMethod().Name);
                    mylog.Log(strData);
                }
                return(true);
            }