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); } } }
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); } } }
/// <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); }