题目地址:http://www.careercup.com/question?id=5678716545925120
Given a string, find the largest repetitive sequence. Algo + Code
Ex: abcdefbcd – bcd, banana – ana
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace find_the_largest_repetitive_sequence 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Do(); 14 } 15 16 static void Do() 17 { 18 Console.WriteLine("please input a string : "); 19 string input = Console.ReadLine(); 20 string result = Find(input); 21 Console.WriteLine(string.Format("result : {0}", result)); 22 Do(); 23 } 24 25 static string Find(string input) 26 { 27 //var array = input.ToCharArray(); 28 int subLength = input.Length - 1; 29 while (subLength > 0) 30 { 31 for (int i = 0; i <= input.Length - subLength; i++) 32 { 33 string tempResult = input.Substring(i, subLength); 34 for (int j = i + 1; j <= input.Length - subLength; j++) 35 { 36 string temp = input.Substring(j, subLength); 37 if (temp.Equals(tempResult, StringComparison.OrdinalIgnoreCase)) 38 { 39 return temp; 40 } 41 } 42 } 43 subLength--; 44 } 45 return ""; 46 } 47 } 48 }