Esempio n. 1
0
        private int[] _getNextVal(StringLink2 s)
        {
            int[] nextVal = new int[s.GetLength() + 1];
            nextVal[1] = 0;
            int i = 1;
            int j = 0;

            for (; i < s.GetLength();)
            {
                if (j == 0 || s[i] == s[j])
                {
                    j++;
                    i++;
                    if (s[i] == s[j])
                    {
                        nextVal[i] = nextVal[j];
                    }
                    else
                    {
                        nextVal[i] = j;
                    }
                }
                else
                {
                    j = nextVal[j];
                }
            }

            return(nextVal);
        }
Esempio n. 2
0
        //替换
        public bool Replace(StringLink2 match, StringLink2 replace, int index = 1)
        {
            if (match.IsEmpty() || replace.IsEmpty() || this.IsEmpty())
            {
                Console.WriteLine("串不能为空!");
                return(false);
            }

            if (index < 1 || index > this.GetLength())
            {
                Console.WriteLine("索引错误!");
                return(false);
            }

            int i = this.Index(match, index);

            if (i != 0)
            {
                this.Delete(i, match.GetLength());
                if (i > this.GetLength())
                {
                    return(this.Concat(replace));
                }
                else
                {
                    return(this.Insert(i, replace));
                }
            }

            return(false);
        }
 static void PrintStringLink2(StringLink2 s, string text = "输出:")
 {
     Console.Write(text);
     for (int i = 1; i <= s.GetLength(); i++)
     {
         char c = '\0';
         s.GetElementBuIndex(i, ref c);
         Console.Write(c);
     }
     Console.WriteLine();
 }
Esempio n. 4
0
        //KMP匹配
        public int Index(StringLink2 match, int index = 1)
        {
            if (match.IsEmpty() || this.IsEmpty())
            {
                Console.WriteLine("串不能为空!");
                return(0);
            }

            if (index < 1 || index > this.GetLength())
            {
                Console.WriteLine("索引错误!");
                return(0);
            }

            int[] nextval = this._getNextVal(match);
            int   i       = index;
            int   j       = 1;

            while (i <= this.GetLength())
            {
                if (j == 0 || this[i] == match[j])
                {
                    i++;
                    j++;
                }
                else
                {
                    j = nextval[j];
                }

                if (j > match.GetLength())
                {
                    return(i - match.GetLength());
                }
            }



            return(0);
        }
        static void TestStringLink2()
        {
            StringLink2 s = new StringLink2(new char[] { 'h', 'e', 'l', 'g', 'o' }, 3);

            PrintStringLink2(s, "串s为:");
            StringLink2 s2 = new StringLink2(new char[] { 'g', 'o' });

            s.Insert(2, new char[] { 'w', 'o' });
            PrintStringLink2(s, "串s在2位置插入wo后:");
            PrintStringLink2(s2, "串s2为:");
            Console.WriteLine("在串s中查找s2的位置为:" + s.Index(s2));
            s.Insert(3, new char[] { 'f', 't' });
            PrintStringLink2(s, "串s在3号位插入ft后:");
            s.Delete(3, 3);
            PrintStringLink2(s, "串s删除3号位后3个字符后:");
            Console.WriteLine();

            s.Replace(s2, new StringLink2(new char[] { 'c', 'h', 'i' }));
            PrintStringLink2(s, "替换go为chi后:");
            s.Concat(new char[] { 'l', 'o', 'v', 'e' });
            PrintStringLink2(s, "链接love后:");
            s.Replace(new StringLink2(new char[] { 'v' }), s2);
            PrintStringLink2(s, "替换v为go后:");
            s.Insert(3, s2);
            PrintStringLink2(s, "3号位插入go后:");
            Console.WriteLine();

            Console.WriteLine("go的位置是:" + s.Index(s2));
            s.Delete(5, 1);
            PrintStringLink2(s, "删除5号位字符后:");
            s.Delete(1, s.GetLength());
            PrintStringLink2(s, "删除全部后:");
            s.ValueTo(new char[] { 'a', 'b', 'c' });
            PrintStringLink2(s, "赋值为abc后:");
            s.Concat(s2);
            PrintStringLink2(s, "链接go后:");
            Console.WriteLine("go的位置为:" + s.Index(s2));
        }
Esempio n. 6
0
 //赋值为串
 public void ValueTo(StringLink2 s)
 {
     this.ValueTo(s.ToChars());
 }
Esempio n. 7
0
 public bool Insert(int index, StringLink2 s)
 {
     char[] chars = s.ToChars();
     return(this.Insert(index, chars));
 }
Esempio n. 8
0
 public bool Concat(StringLink2 s)
 {
     char[] chars = s.ToChars();
     return(this.Concat(chars));
 }