聊城网站建设哪家便宜,做的网站电脑上跟手机上不一样,4s店网站建设计划,个人网站代做description
给出主串s和模式串t#xff0c;其长度均不超过1000。本题要求实现一个函数BF(string s, string t)#xff0c;求出模式串t在主串s中第一次出现的位置#xff08;从0开始计算#xff09;#xff0c;如果在s中找不到t#xff0c;则输出-1。
函数接口定义其长度均不超过1000。本题要求实现一个函数BF(string s, string t)求出模式串t在主串s中第一次出现的位置从0开始计算如果在s中找不到t则输出-1。
函数接口定义 /* s为主串t为模式串。
函数返回t在s中第一次出现的位置。 */ int BF(string s, string t); 其中 s 和 t 分别为主串和模式串长度均不超过1000。函数返回模式串t在主串s中第一次出现的位置从0开始计算如果在s中找不到t则输出-1。
裁判测试程序样例
#include bits/stdc.h
using namespace std;/* s为主串t为模式串。* 函数返回t在s中第一次出现的位置。*/
int BF(string s, string t);int main(int argc, char const *argv[])
{string s, t;getline(cin, s); //输入主串getline(cin, t); //输入模式串int pos BF(s, t); //搜索cout pos endl;//输出模式串在主串中第一次出现的位置return 0;
}/* 请在这里填写答案 */输入样例1
This is a test string is
输出样例1
2
输入样例2
This is a test string The
输出样例2
-1
solution
int BF(string s, string t){for(int i 0; i s.length(); i){int temp i, j 0;while(s[temp] t[j] j t.length() temp s.length()){temp;j;}if(j t.length()) return i;}return -1;
}