[编程题]BFS
  • 热度指数:25763 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}Bob 在学习了 DFS 后,自己又发明了一种新的搜(luan)索(gao)方法,叫做 BFS(Bob First Search)。
\hspace{15pt}这种搜索被定义为:在一个字符串中,从前向后查找子串 "Bob" 第一次出现的位置(不区分大小写)。

输入描述:
\hspace{15pt}在一行输入一个不含空格的字符串 S,其长度为 |S|,满足 \left(1 \leqq |S| \leqq 100\right)


输出描述:
\hspace{15pt}输出一个整数,表示子串 "Bob" 第一次出现的位置(下标从 0 开始)。 
\hspace{15pt}如果子串未出现,则输出 -1
示例1

输入

Bobob

输出

0

说明

字符串 "Bobob" 中开头即出现 "Bob",起始索引为 0
示例2

输入

bobby

输出

0

说明

字符串 "bobby" 中开头即出现 "bob",起始索引为 0
示例3

输入

body

输出

-1

说明

字符串 "body" 中不包含子串 "Bob"(忽略大小写),因此输出 -1
s = input().lower()
print(s.find('bob'))
发表于 2025-07-22 20:00:24 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main() {
   string s;
   cin>>s;
   for(auto &i:s){
    i=tolower(i);
   }
   char c[]="Bob";
   for(auto &i:c){
    i=tolower(i);
   }
   int pos=s.find(c);
   cout<<pos;
}
发表于 2025-09-21 16:26:41 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    char str[101];
    scanf("%s", str);
    if (strlen(str) <= 2) {
        printf("-1");
    } else if (strlen(str) > 2) {
        int num = -1;
        for (int i = 0; i <= strlen(str) - 3; i++) {
            if ((str[i] == 'B' || str[i] == 'b')
                    && (str[i + 1] == 'o' || str[i + 1] == 'O')
                    && (str[i + 2] == 'b' || str[i + 2] == 'B')) {
                num = i;
                break;
            }
        }
        printf("%d", num);
    }
    return 0;
}


注意一下&&跟||的优先级,&&先进行判断,所以要用下(),还有就是要讨论一下这个字符串的长度,小于3的直接输出-1.
没了。
🐒🐒🐒🐒🐒🐒
发表于 2025-12-14 09:29:48 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String S = in.nextLine();
        System.out.println(S.toLowerCase().indexOf("bob"));
    }
}

发表于 2025-10-10 13:09:04 回复(0)
    string s;
    cin >> s;
    for (auto& i : s) {
        i = tolower(i);
    }
   string c = "bob";
    int pos = s.find(c);
    cout << pos;

发表于 2026-01-23 16:10:38 回复(0)
#include<stdio.h>
#include<string.h>
int main()
{
    int len;
    char a[101];
    scanf("%s",a);
    len=strlen(a);
    int found=0;
    for(int i=0;i<len-2;i++)
    {
        if((a[i]=='b'||a[i]=='B')&&(a[i+1]=='o'||a[i+1]=='O')&&(a[i+2]=='b'||a[i+2]=='B'))
        {
        printf("%d",i);
        found=1;
        break;
        }}
        if(found==0)
        printf("-1");
       
   
    return 0;
}
发表于 2025-12-18 21:20:13 回复(0)
学会遍历就行
#include <stdio.h>

int main() {
	char s[101]={0};
	scanf("%s",s);
	int a=sizeof(s)/sizeof(char);
	int n=0;
	for(int i=0;i<=a-2;i++){
		if((s[i]=='B'||s[i]=='b')&&((s[i+1])=='o'||(s[i+1]=='O'))&&((s[i+2])=='b'||(s[i+2])=='B')){
			printf("%d\n",i);
			n=1;
			break;
		}
	}
	if(!n){
		printf("-1\n");
	}
	return 0;
}

发表于 2025-12-18 20:19:16 回复(0)

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    int a=0;
	cin>>s; 
    if (s.size()>=3){
    for (int i=0;i<=s.size()-3;i++){
        if((s[i]=='b'|| s[i]=='B')&& (s[i+1]=='o'|| s[i+1]=='O' )&&( s[i+2]=='b'|| s[i+2]=='B')){
            cout<<i;
            a=1;
            break;
        }
       
    }
    }
    if (a==0) cout<<"-1";
}

发表于 2025-10-13 11:20:00 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
            String s = in.next();
            String noCaseS =s.toLowerCase();
            int pos = noCaseS.indexOf("bob");
            System.out.println(pos);
    }
}

发表于 2026-04-06 18:02:07 回复(0)
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(){
    char s1[101];
    scanf("%s",s1);
    char lower[101];
    for(int i=0;s1[i];i++){
        lower[i]=tolower(s1[i]);
    }
    lower[strlen(s1)]='\0';
    char s2[4]={"bob"};
    char*p=strstr(lower,s2);
    if(p) printf("%ld",p-lower);
    else printf("-1");
    return 0;
}
发表于 2026-04-01 09:29:14 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s=in.next();
        String c="bob";
        s=s.toLowerCase();
        for(int a=0;a<s.length()-2;a++){
            if(c.equals(s.substring(a,a+3))){
                System.out.print(a);
                return;
            }
        }
        System.out.print(-1);
    }
}
发表于 2026-03-28 11:43:08 回复(0)
S = input().lower()
B = "bob"
if B not in S:
    print(-1)
elif B in S:
    for i in range(len(S) - 2):
        if S[i:i + 3] == B:
            print(i)
            break
        else:
            continue
发表于 2026-03-13 13:41:58 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str;
    cin>>str;
    int n = -1;
    for(char& c:str)
    c = tolower(c);
    n = str.find("bob");
    cout<<n;
}
发表于 2026-03-03 17:28:05 回复(0)
#include<bits/stdc++.h>
#include<vector>
#define sc scanf
#define pf printf
#define ll long long
#define isprime pri
#define ld long double
#define D double
using namespace std;
typedef pair<int,int> p;
const int manx = 1e8 + 10;
int primes[manx],pp = 0;
bitset <manx> yy;
inline void gt(){
   
}
int main(){
    string s;
    cin >> s;
    int len = s.length();
    int index = -1;
    for (int i = 0 ;i <= len - 3;i++){
        char c = tolower(s[i]);
        char c1 = tolower(s[i + 1]);
        char c2 = tolower (s[i + 2]);
        if(c == 'b' && c1 == 'o' && c2 == 'b'){
            index = i;
            break;
        }
    }
    cout << index;
    return 0;
}
发表于 2026-02-24 16:07:56 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main() {
string s;
cin >> s;
int n = s.size();
int result = -1;

// 遍历字符串,检查每个可能的起始位置(避免越界:i <= n-3)
for (int i = 0; i <= n - 3; ++i) {
// 手动判断:第1位是B或b,第2位是O或o,第3位是B或b
bool first = (s[i] == 'B' || s[i] == 'b'); // 第一个字符匹配B/b
bool second = (s[i+1] == 'O' || s[i+1] == 'o'); // 第二个字符匹配O/o
bool third = (s[i+2] == 'B' || s[i+2] == 'b'); // 第三个字符匹配B/b
// 三个字符都匹配时,记录位置并退出循环
if (first && second && third) {
result = i;
break; // 找到第一次出现的位置就停止
}
}

cout << result << endl;
return 0;
}

发表于 2026-02-09 15:28:32 回复(0)
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
    string s;
    cin >> s;

    int result = -1;

    for (int i = 0; i <= (int)s.length() - 3; i++) {
        // 不区分大小写检查 "Bob"
        if (tolower(s[i]) == 'b' &&
                tolower(s[i + 1]) == 'o' &&
                tolower(s[i + 2]) == 'b') {
            result = i;
            break;
        }
    }

    cout << result << endl;
    return 0;
}

发表于 2026-02-07 20:46:08 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main()
{
    char s[100000];
    cin>>s;
    int i,k,a=strlen(s);
    for(i=0;s[i]!='\0';i++)
    {
        k=i;
        if(s[i]=='B'||s[i]=='b')
        {
            if(s[i+1]=='o'||s[i+1]=='O')
            {
                if(s[i+2]=='b'||s[i+2]=='B')
                {
                    cout<<i;
                    break;
                }
            }
        }

    }
    if(k+1==a)
        cout<<"-1";

    return 0;
}










发表于 2026-02-01 13:09:28 回复(0)
#include<stdio.h>
int main()
{
    char str[101];//fgets函数会读取换行符,所以字符串长度上限为101
    int z = 0;
    //printf("请输入一个字符串,将会找到第一个出现Bob的位置(不区分大小写):");
    fgets(str, sizeof(str), stdin);
    for (int i = 0; i < 98; i++)
    {
        if ((str[i] == 'B' || str[i] == 'b') && (str[i + 1] == 'o'||str[i + 1] == 'O') && (str[i + 2] == 'b'||str[i + 2] == 'B'))
        {
            z = 1;
            printf("%d", i);
            break;
        }
    }
    if (z == 0)
    printf("-1");
    return 0;
}
发表于 2026-01-28 10:50:46 回复(0)
#include <cctype>
#include <iostream>
using namespace std;

int main() {
    string s;
    cin>>s;
    int pos=0;
    for(int i=0;i<s.size();i++){
        s[i]=tolower(s[i]);
        pos=s.find("bob");
    }
    if(pos>=0) cout<<pos<<endl;
    else cout<<"-1"<<endl;
}

发表于 2026-01-27 22:19:21 回复(0)
lst = (input())
out = -1
for i in range(len(lst)-2):
    if lst[i] == 'b' or lst[i] == 'B':
        if lst[i+1] == 'o' or lst[i+1] == 'O':
            if lst[i+2] == 'b' or lst[i+2] == 'B':
                out = i
                break
print(out)

发表于 2026-01-23 16:35:47 回复(0)