该程序不提供输出。

时间:2016-03-30 15:17:48

标签: c# boyer-moore

我用C#代码编写了这个Boyer Moore算法,但是当我输入文本和模式时,它并没有给出任何解决方案。 我得到了一个我必须实现的算法。我在代码中做了一些逻辑但是它不会给出解决方案。

static void Main(string[] args)
    {
        Console.WriteLine("Enter String: ");
        string t = Console.ReadLine();
        Console.WriteLine("Enter pattern: ");
        string p = Console.ReadLine();
        string sigma = new String(p.Distinct().ToArray());

        Program pro = new Program();
        pro.BoyerMooreMatcher(t,p,sigma);
        Console.ReadKey();
    }
    public void BoyerMooreMatcher(string t,string p , string sigma) {
     Program pro = new Program();
    int n= t.Length;
    int m = p.Length;
  int[] lambda = pro.badCharacter(p,m,sigma);

    int[] gamma = pro.GoodSuffix(p,m);
    m = m - 1;
    //n = n - 1;
    int s = 0;
        int j=0;

        while(s<=n-m){
            j = m;
            while(j>0 && p[j]==t[s+j]){

                j = j - 1;
                if (j == 0)
                {
                    Console.WriteLine("patter occured at shift ", s);
                    s = s + gamma[0];
                }
                else
                {
                    s = s + Math.Min(gamma[j], j = lambda[t[s + j]]);
                }

                }                                
            }
        }

    public int[] badCharacter(string P, int m, string sigma)
    {
        int[] alpha = new int[256];
        int j;

        foreach(char a in sigma)
        {
            alpha[a] = 0;

        }
        for (j = 0; j <= m-1; j++)
        {
            alpha[P[j]] = j;
        }
        return alpha;
    }

    public int[] GoodSuffix(string P, int m)
    {
      int[] pie = Prefix(P);

     char[] chararray = P.ToCharArray();
     Array.Reverse(chararray);

     string pinv = new string(chararray);
      int[] pieinv = Prefix(pinv);

        int[] gamma= new int[100];
        int j;


         Prefix(pinv.ToString());

        for (j = 0; j <= m; j++)
        {
            gamma[j] = m - pie[m];

        }
        for (int l = 1; l <= m;l++)
        {
            j = m - pieinv[l];
            if (gamma[j] > l - pieinv[l]) {
                gamma[j] = l - pieinv[l];
            }
        }
        return gamma;
    }

    public int[] Prefix(string P)
    {
      int   m = P.Length;
        int k;

        int[] pie= new int[m+1];
       for (int i = 1; i <= m; i++)
       {
           pie[i] = i;
        }
  //     m = m - 1;

        pie[1] = 0;
        k = 0;

        for (int q = 2; q <= m-1; q++)
        {
            while (k > 0 && P[k + 1] != P[q])
            {
                k = pie[k];
            }
            if (P[k + 1] == P[q])
            {
                k = k + 1;
            }
            pie[q] = k;
        }
        return pie;
    }

0 个答案:

没有答案