给定两个字符串S和T,如何用Java代码,利用BF算法,在主串S中查找子串T。并写出伪码
时间: 2024-05-30 16:12:28 浏览: 17
以下是Java代码实现BF算法查找子串T在主串S中出现的位置:
public static int bfSearch(String s, String t) {
int sLen = s.length();
int tLen = t.length();
for(int i = 0; i < sLen - tLen + 1; i++) {
int j = 0;
while(j < tLen && s.charAt(i + j) == t.charAt(j)) {
j++;
}
if(j == tLen) {
return i;
}
}
return -1;
}
伪码如下:
BF算法查找子串T在主串S中出现的位置:
1. 比较主串S[i]和子串T[j],如果相等则继续比较后面的字符
2. 如果不相等,则i指针和j指针都回到初始位置重新开始比较
3. 直到j指针到达子串T的末尾,说明查找成功,返回子串T在主串S中的位置i
4. 如果i指针已经比较到主串S的末尾仍然没有找到子串T,则查找失败,返回-1
相关问题
给定两个字符串S和T,如何用Java代码,利用BF算法,在主串S中查找子串T
的位置?
很高兴见到你!关于你的问题,我可以给出以下Java代码,基于暴力匹配算法(Brute Force,BF):
```
public class BFAlgorithm {
public static int bFAlgo(String s, String t) {
int i = 0, j = 0;
while (i < s.length() && j < t.length()) {
if (s.charAt(i) == t.charAt(j)) { // 如果匹配,则主串和模式串下标均后移一位
i++;
j++;
} else { // 否则找下一个主串起点位置
i = i - j + 1;
j = 0;
}
}
if (j == t.length()) { // 字符串完全匹配
return i - j;
} else {
return -1;
}
}
public static void main(String[] args) {
String s = "abcdefg";
String t = "def";
int index = bFAlgo(s, t);
System.out.println(index);
}
}
```
如果子串T在主串S中出现,返回其起始位置的下标;否则返回-1。需要注意的是,这种方法的时间复杂度为O(mn),其中m和n分别为主串和模式串的长度,因此它在实际应用中可能不太适合处理大量数据。希望这可以帮到你!
(1)给定两个字符串 S 和 T,利用 BF 算法,在主串 S 中查找子串 T,并输出查找结果,输出时 需以文字形式对查找结果进行定性说明; (2)要求以 C++/C/Java 等编程语言进行实现; (3)建立算法时间复杂度分析模型,并利用计算机统计该算法的执行效率。用c++完成
下面是C++代码实现BF算法查找子串T在主串S中的位置:
```
#include <iostream>
#include <string>
using namespace std;
int BF(string S, string T) {
int i = 0, j = 0;
int lenS = S.length(), lenT = T.length();
while (i < lenS && j < lenT) {
if (S[i] == T[j]) {
i++;
j++;
} else {
i = i - j + 1;
j = 0;
}
}
if (j == lenT) {
return i - j;
} else {
return -1;
}
}
int main() {
string S = "hello world";
string T = "world";
int pos = BF(S, T);
if (pos == -1) {
cout << "未找到子串T" << endl;
} else {
cout << "子串T在主串S中的位置为" << pos << endl;
}
return 0;
}
```
时间复杂度分析:
最坏情况下,需要比较的次数为 $(n-m+1)m$,其中 $n$ 为主串长度,$m$ 为子串长度。因此,BF算法的时间复杂度为 $O(nm)$。
执行效率统计:
可以使用计时函数进行统计,计算出程序执行的时间,从而得到算法的执行效率。如下所示:
```
#include <iostream>
#include <string>
#include <ctime> //计时函数头文件
using namespace std;
int BF(string S, string T) {
int i = 0, j = 0;
int lenS = S.length(), lenT = T.length();
while (i < lenS && j < lenT) {
if (S[i] == T[j]) {
i++;
j++;
} else {
i = i - j + 1;
j = 0;
}
}
if (j == lenT) {
return i - j;
} else {
return -1;
}
}
int main() {
string S = "hello world";
string T = "world";
int pos;
clock_t start, stop;
start = clock(); //开始计时
pos = BF(S, T);
stop = clock(); //停止计时
if (pos == -1) {
cout << "未找到子串T" << endl;
} else {
cout << "子串T在主串S中的位置为" << pos << endl;
}
cout << "执行时间为" << double(stop - start) * 1000 / CLOCKS_PER_SEC << "ms" << endl; //计算程序执行时间
return 0;
}
```
这里使用了 `clock()` 函数来计时,其中 `CLOCKS_PER_SEC` 表示每秒钟的计时周期数。执行效率可以根据具体的硬件环境和测试数据进行统计。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)