Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. So I Wrote A Backward Version Of KMP Algorithm In C# That Searches String From Right

So I Wrote A Backward Version Of KMP Algorithm In C# That Searches String From Right

Scheduled Pinned Locked Moved Algorithms
algorithmscsharpcomannouncement
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Vandenberg Huang
    wrote on last edited by
    #1
        public static int LastIndexOf(this IReadOnlyList s, IReadOnlyList t, int startIndex, 
            IEqualityComparer comparer) where T : IEquatable
        {
            Validate(s, t, startIndex);
    
            // Follow the behavior of string.LastIndexOf(string) method. 
            if (t.Count == 0) return 0;
            if (s.Count == 0 || s.Count < t.Count) return -1;
    
            if (comparer == null) comparer = EqualityComparer.Default;
            if (t.Count == 1) return LastIndexOf(s, t\[0\], startIndex, comparer);
    
            var table = BuildTable(t, comparer);
            var i = 0;  
    
            while (startIndex - i >= 0)  
            {
                if (comparer.Equals(t\[t.Count - i - 1\], s\[startIndex - i\]))
                {
                    if (i == t.Count - 1)
                        return startIndex - t.Count + 1;
                    i++;
                }
                else
                {
                    if (table\[i\] > -1)
                    {
                        startIndex -= i;
                        i = table\[i\];
                    }
                    else
                    {
                        startIndex--;
                        i = 0;
                    }
                }
            }
            return -1;
        }
    

    Full source code can be seen here[^]. The method works like String.LastIndexOf which searches string from the last character. The BuildTable method is exactly same with normal KMP algorithm. I'm not sure if this is the best solution though.

    L 1 Reply Last reply
    0
    • R Robert Vandenberg Huang
          public static int LastIndexOf(this IReadOnlyList s, IReadOnlyList t, int startIndex, 
              IEqualityComparer comparer) where T : IEquatable
          {
              Validate(s, t, startIndex);
      
              // Follow the behavior of string.LastIndexOf(string) method. 
              if (t.Count == 0) return 0;
              if (s.Count == 0 || s.Count < t.Count) return -1;
      
              if (comparer == null) comparer = EqualityComparer.Default;
              if (t.Count == 1) return LastIndexOf(s, t\[0\], startIndex, comparer);
      
              var table = BuildTable(t, comparer);
              var i = 0;  
      
              while (startIndex - i >= 0)  
              {
                  if (comparer.Equals(t\[t.Count - i - 1\], s\[startIndex - i\]))
                  {
                      if (i == t.Count - 1)
                          return startIndex - t.Count + 1;
                      i++;
                  }
                  else
                  {
                      if (table\[i\] > -1)
                      {
                          startIndex -= i;
                          i = table\[i\];
                      }
                      else
                      {
                          startIndex--;
                          i = 0;
                      }
                  }
              }
              return -1;
          }
      

      Full source code can be seen here[^]. The method works like String.LastIndexOf which searches string from the last character. The BuildTable method is exactly same with normal KMP algorithm. I'm not sure if this is the best solution though.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I would have just reversed the 2 strings and compared those. (KMP: Keep Me Partying). ;P

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups