标签:
static void Main(string[] args) { char[] pattern = { ‘a‘, ‘b‘, ‘c‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ }; int[] table = new int[pattern.Length]; cal_next_table(pattern, table); Console.Read(); } static void cal_next_table(char[] pattern,int[] table) { //设Table[0]为哨兵 table[0] = 1; Print(pattern); Print(table); for (var i = 1; i < pattern.Length; i++) { table[i] = table[i - 1]; var t1 = GetSubArr(pattern, 0, i); //检测t2(注意起点)是否和t1头部重合,重合则break for(var j=table[i];j<i;j++) { var t2 = GetSubArr(t1, j, i ); Print(t1); if (!strcncmp(t1, t2)) { table[i]++; } else break; } Print(t1); Print(pattern); Print(table); } } static char[] GetSubArr(char[] pattern,int start,int end) { char[] t = new char[end - start]; for(int i=0;i<t.Length;i++) { t[i] = pattern[i + start]; } return t; } static void Print(Array arr) { foreach(var a in arr) { Console.Write(a + " "); } Console.WriteLine(); } static bool strcncmp(char[] arr1,char[] arr2) { for(var i=0;i<arr2.Length;i++) { if (arr1[i] != arr2[i]) return false; } return true; }
6.结果
a b c a b c d e f 1 0 0 0 0 0 0 0 0 a a b c a b c d e f 1 1 0 0 0 0 0 0 0 b a b a b c a b c d e f 1 1 2 0 0 0 0 0 0 c a b c a b c a b c d e f 1 1 2 3 0 0 0 0 0 a a b c a a b c a b c d e f 1 1 2 3 3 0 0 0 0 a b a b c a b a b c a b c d e f 1 1 2 3 3 3 0 0 0 a b c a b c a b c a b c a b c d e f 1 1 2 3 3 3 3 0 0 a b c d b c d c d d a b c a b c d a b c a b c d e f 1 1 2 3 3 3 3 7 0 e a b c a b c d e a b c a b c d e f 1 1 2 3 3 3 3 7 8
标签:
原文地址:http://www.cnblogs.com/WongSiuming/p/5119943.html