首页 > 试题广场 >

String Matching

[编程题]String Matching
  • 热度指数:11296 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs.     Typically,the text is a document being edited,and the pattern searched for is a particular word supplied by the user.       We assume that the text is an array T[1..n] of length n and that the pattern is an array P[1..m] of length m<=n.We further assume that the elements of P and  T are all alphabets(∑={a,b...,z}).The character arrays P and T are often called strings of characters.       We say that pattern P occurs with shift s in the text T if 0<=s<=n and T[s+1..s+m] = P[1..m](that is if T[s+j]=P[j],for 1<=j<=m).       If P occurs with shift s in T,then we call s a valid shift;otherwise,we calls a invalid shift.      Your task is to calculate the number of vald shifts for the given text T and p attern P.

输入描述:
   For each case, there are two strings T and P on a line,separated by a single space.You may assume both the length of T and P will not exceed 10^6.


输出描述:
    You should output a number on a separate line,which indicates the number of valid shifts for the given text T and pattern P.
示例1

输入

abababab abab

输出

3
头像 普罗列塔丽亚
发表于 2022-02-01 21:06:03
注意,find找到后不光要sum++,还要i=pos,否则会重复查找 continue用来跳过i++ #include<iostream> #include<string> using namespace std; int  展开全文
头像 csyfZhang
发表于 2020-04-24 12:59:54
给定字符串T和P,求出P在T中出现的次数【KMP模板题】 https://blog.csdn.net/csyifanZhang/article/details/105728330↑更好的阅读体验 这道题暴力很容易想到,不断的在T中找P的第一个元素,一旦找到了就看看是否T接下来的元素和P匹配,乍一看 展开全文
头像 ysong想养只修狗
发表于 2023-05-04 16:16:55
#include <iostream> using namespace std; const int N = 1e6 + 10; //注:以下数组下标从1开始 char p[N], s[N]; int ne[N]; void myNext(){//创建next数组 int 展开全文
头像 牛客440904392号
发表于 2024-09-30 01:36:39
//Java版代码 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); 展开全文
头像 T790T
发表于 2024-07-31 08:46:31
#include <iostream> using namespace std; int main() { string t,p; while(cin>>t>>p){ int count=0; for(int i= 展开全文
头像 chong_0428
发表于 2024-03-19 18:12:52
def getNext(t): next = [0] * (len(t)+1) t = list(t) i, k = 0, -1 next[0] = -1 while i < len(t): if k == -1 or t[i] == t 展开全文
头像 风度翩翩的zzp很谦虚
发表于 2023-02-20 19:05:29
#include <iostream> #include <string> #include <vector> using namespace std; vector<int> GetNextTable(const string& p){ / 展开全文
头像 SStarry
发表于 2023-08-04 09:52:41
#include <iostream> #include <cstring> using namespace std; string a,b; int main() { cin >> a >> b; int j, t, count 展开全文
头像 牛客892605956号
发表于 2024-03-21 22:35:26
#include <iostream> #include <string> using namespace std; int main() { string a, b; while (cin >> a >> b) { 展开全文
头像 JXH001
发表于 2024-02-19 10:24:51
#include<iostream> #include<string> using namespace std; int nextable[1000]; void getnext(string a, int n1) { int j = 0; nextable[j] = 展开全文

问题信息

难度:
63条回答 6971浏览

热门推荐

通过挑战的用户

查看代码
String Matching