字符串查找函数(BF)
//模拟字符串定位函数
// s: abcbbghi
// t: ghi
// 返回6 #include <iostream>
#include <string>
#include <algorithm> using namespace std; int main()
{
string s, t;
int len1, len2;
int i, j; while(cin>>s)
{
cin>>t;
len1=s.size();
len2=t.size();
i=0; j=0;
while(i<len1 && j<len2 )
{
if(s[i]==t[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
}
if(j>=len2)
{
cout<<i-j+1<<endl;
}
else
{
cout<<"No\n";
}
}
return 0;
}
字符串查找函数(BF)的更多相关文章
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- C/C++字符串查找函数
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- C/C++字符串查找函数 <转>
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...
- 字符串查找函数 find()函数
find()函数可以帮助你在两个字符串之间,查找很多他们的关系... #include<iostream> #include<string> using namespace s ...
- c++ 字符串查找函数
头文件:#include <string.h> 定义函数:int strcasecmp (const char *s1, const char *s2); 函数说明:strcasecmp( ...
- Lua 字符串查找函数 string.find(s, pattern [, init [, plain]] )【转】
函数原型 string.find(s, pattern [, init [, plain]] ) s: 源字符串 pattern: 待搜索模式串 init: 可选, 起始位置 plain: 我没用过 ...
- 浅析JavaScript的字符串查找函数:indexOf和search
语法 ①indexOf:方法可返回某个指定的字符串值在长字符串中首次出现的位置.如果被查找字符串没有找到,返回-1. indexOf 说明:该方法将从头到尾地检索字符串 stringObject,看它 ...
- 【pyhon】Python里的字符串查找函数find和java,js里的indexOf相似,找到返回序号,找不到返回-1
# 例子: target='www.163.com' ')) ')==-1: print('263不存在于字符串'+target+'中') 运行: C:\Users\horn1\Desktop\pyt ...
随机推荐
- POJ1861 Network
Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %lld & %llu Description Andrew is ...
- msp430项目编程56
msp430综合项目---扩展项目六56 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- Scrapy学习-4-Items类&Pipelines类
items类使用 作用 能使得我们非常方便的操作字段名 在items.py中定制我们的类 class ArticleItem(scrapy.Item): title = scrapy.Field() ...
- 最短路中部分点只能从中任意选取K个问题
题意:给N个点,还有另外m个点(其中只能选K个),求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其他点了),若该点是m个点中的,则count[i]=原来的+1:若不是,则 ...
- hdu - 3836 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- 一个球,初始高度100,每次落下回弹一半高度,求第n次落下球走的距离
def get_height(n): if n==1: eturn 150 return 100+sum([200*pow(0.5,i) for i in range(1,n)])+100*pow(0 ...
- Python机器学习--回归
线性回归 # -*- coding: utf-8 -*- """ Created on Wed Aug 30 19:55:37 2017 @author: Adminis ...
- ScSPM
Linear Spatial Pyramid Matching using Sparse Coding for Image Classification (CVPR'09) 稀疏编码系列: (一)-- ...
- BUPT复试专题—哈夫曼编码(2009)
题目描述 哈夫曼编码中 平均码长=码长×码字出现的概率 如:ABCDE 五个字符的出现次数分别为50 20 5 10 15 那么,其哈夫曼编码为A:0 B:10 C:1110 D:111 ...
- SpringCloud中Redis的使用
1.引入redis相关jar包 <dependency> <groupId>org.springframework.boot</groupId> <artif ...