コード例 #1
0
        /// <summary>
        /// Returns the longest common string of the two specified strings.
        /// </summary>
        /// <param name="s">one string</param>
        /// <param name="t">the other string</param>
        /// <returns>the longest common string that appears as a substring
        /// in both <c>s</c> and <c>t</c>; the empty string
        /// if no such string</returns>
        ///
        public static string Lcs(string s, string t)
        {
            SuffixArray suffix1 = new SuffixArray(s);
            SuffixArray suffix2 = new SuffixArray(t);

            // find longest common substring by "merging" sorted suffixes
            string lcs = "";
            int    i = 0, j = 0;

            while (i < s.Length && j < t.Length)
            {
                int    p = suffix1.Index(i);
                int    q = suffix2.Index(j);
                string x = lcp(s, p, t, q);
                if (x.Length > lcs.Length)
                {
                    lcs = x;
                }
                if (compare(s, p, t, q) < 0)
                {
                    i++;
                }
                else
                {
                    j++;
                }
            }
            return(lcs);
        }
コード例 #2
0
ファイル: SuffixArray.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            Regex  WhiteSpace = new Regex(@"[\s]+", RegexOptions.Compiled);
            string s          = StdIn.ReadAll().Trim();

            s = WhiteSpace.Replace(s, " ");
            SuffixArray suffix = new SuffixArray(s.Trim());

            Console.WriteLine("  i ind lcp rnk select");
            Console.WriteLine("---------------------------");

            for (int i = 0; i < s.Length; i++)
            {
                int    index = suffix.Index(i);
                string ith   = "\"" + s.Substring(index, Math.Min(index + 50, s.Length) - index) + "\"";
                Debug.Assert(s.Substring(index).Equals(suffix.Select(i)));
                int rank = suffix.Rank(s.Substring(index));
                if (i == 0)
                {
                    Console.WriteLine("{0,3} {1,3} {2,3} {3,3} {4}", i, index, "-", rank, ith);
                }
                else
                {
                    int lcp = suffix.Lcp(i);
                    Console.WriteLine("{0,3} {1,3} {2,3} {3,3} {4}", i, index, lcp, rank, ith);
                }
            }
        }
コード例 #3
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            Regex  WhiteSpace = new Regex(@"[\s]+", RegexOptions.Compiled);
            string text       = StdIn.ReadAll();

            text = WhiteSpace.Replace(text, " ");
            SuffixArray suffix = new SuffixArray(text.Trim());

            //Console.WriteLine(text);
            Console.WriteLine("'" + LongestRepeatedSubstring.Lrs(text) + "'");
        }
コード例 #4
0
ファイル: SuffixArrayX.cs プロジェクト: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            Regex  WhiteSpace = new Regex(@"[\s]+", RegexOptions.Compiled);
            string s          = StdIn.ReadAll().Trim();

            s = WhiteSpace.Replace(s, " ");

            SuffixArray  suffix2 = new SuffixArray(s);
            SuffixArrayX suffix1 = new SuffixArrayX(s);
            bool         check   = true;

            for (int i = 0; check && i < s.Length; i++)
            {
                if (suffix1.Index(i) != suffix2.Index(i))
                {
                    Console.WriteLine("suffix1(" + i + ") = " + suffix1.Index(i));
                    Console.WriteLine("suffix2(" + i + ") = " + suffix2.Index(i));
                    string ith = "\"" + s.Substring(suffix1.Index(i), Math.Min(suffix1.Index(i) + 50, s.Length) - suffix1.Index(i)) + "\"";
                    string jth = "\"" + s.Substring(suffix2.Index(i), Math.Min(suffix2.Index(i) + 50, s.Length) - suffix2.Index(i)) + "\"";
                    Console.WriteLine(ith);
                    Console.WriteLine(jth);
                    check = false;
                }
            }

            Console.WriteLine("  i ind lcp rnk  select");
            Console.WriteLine("---------------------------");

            for (int i = 0; i < s.Length; i++)
            {
                int    index = suffix2.Index(i);
                string ith   = "\"" + s.Substring(index, Math.Min(index + 50, s.Length) - index) + "\"";
                Debug.Assert(s.Substring(index).Equals(suffix2.Select(i)));
                int rank = suffix2.Rank(s.Substring(index));

                if (i == 0)
                {
                    Console.Write("{0,3} {1,3} {2,3} {3,3} {4}\n", i, index, "-", rank, ith);
                }
                else
                {
                    // int lcp  = suffix.lcp(suffix2.index(i), suffix2.index(i-1));
                    int lcp = suffix2.Lcp(i);
                    Console.Write("{0,3} {1,3} {2,3} {3,3} {4}\n", i, index, lcp, rank, ith);
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Returns the longest repeated substring of the specified string.</summary>
        /// <param name="text">the string</param>
        /// <returns>the longest repeated substring that appears in <c>text</c>;
        ///        the empty string if no such string</returns>
        ///
        public static string Lrs(string text)
        {
            int         N   = text.Length;
            SuffixArray sa  = new SuffixArray(text);
            string      lrs = "";

            for (int i = 1; i < N; i++)
            {
                int length = sa.Lcp(i);
                if (length > lrs.Length)
                {
                    lrs = text.Substring(sa.Index(i), sa.Index(i) + length - sa.Index(i));
                }
            }
            return(lrs);
        }
コード例 #6
0
        public static void MainTest(string[] args)
        {
            Regex WhiteSpace = new Regex(@"[\s]+", RegexOptions.Compiled);

            TextInput input   = new TextInput(args[0]);
            int       context = int.Parse(args[1]);

            // read in text
            string text = input.ReadAll();

            text = WhiteSpace.Replace(text, " ");
            int N = text.Length;

            // build suffix array
            SuffixArray sa    = new SuffixArray(text);
            TextInput   StdIn = new TextInput();

            // find all occurrences of queries and give context
            while (StdIn.HasNextLine())
            {
                string query = StdIn.ReadLine();
                for (int i = sa.Rank(query); i < N; i++)
                {
                    int from1 = sa.Index(i);
                    int to1   = Math.Min(N, from1 + query.Length);
                    if (!query.Equals(text.Substring(from1, to1 - from1)))
                    {
                        break;
                    }
                    int from2 = Math.Max(0, sa.Index(i) - context);
                    int to2   = Math.Min(N, sa.Index(i) + context + query.Length);
                    Console.WriteLine(text.Substring(from2, to2 - from2));
                }
                Console.WriteLine();
            }
        }