子串算法 [英] Substring algorithm

查看:139
本文介绍了子串算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能向我解释如何解决子问题反复?

Can someone explain to me how to solve the substring problem iteratively?

的问题:给定两个字符串S=S1S2S3SnT=T1T2T3Tm,与 M 的小于或等于的 N 的,确定的 T 的是取值的子字符串。

The problem: given two strings S=S1S2S3Sn and T=T1T2T3Tm, with m is less than or equal to n, determine if T is a substring of S.

推荐答案

不知道你正在工作,但这里的语言是C#中的一个例子。这是一个大致ñ 2 的算法,但它会完成这项工作。

Not sure what language you're working in, but here's an example in C#. It's a roughly n2 algorithm, but it will get the job done.

bool IsSubstring (string s, string t)
{
   for (int i = 0; i <= (s.Length - t.Length); i++)
   {
      bool found = true;

      for (int j = 0; found && j < t.Length; j++)
      {
         if (s[i + j] != t[j])
             found = false;
      }

      if (found)
         return true;
   }

   return false;
}

这篇关于子串算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆