Exemple #1
0
        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);
                }
            }
        }
Exemple #2
0
        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);
                }
            }
        }
Exemple #3
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();
            }
        }