0

0

查询字符串A中是否存在字符串B作为子字符串

WBOY

WBOY

发布时间:2023-09-03 12:25:10

|

1204人浏览过

|

来源于tutorialspoint

转载

查询字符串a中是否存在字符串b作为子字符串

介绍

In this tutorial, we will see queries to check if string B exists as a substring of string A. A substring is a string that is part of the main string. In the Query array, there are some integer values, and the index of string A will be checked to see if those integer values match the substring B or not.We use C++ queries to find out whether B is a substring of A or not.In this approach, there is a string A and B is the substring of A. Queries in C++ are integer values represented in array form. There is a string A, B is the substring, and i is the integer value of some queries. If substring B is present in string A at query index values, the output will be Yes, otherwise, the output is No.

Implementation 1

的中文翻译为:

实施方案1

String A = “ababababa”
Substring B = “aba”
Queries[] = {0,1, 2, 3}

Output

A[0,2] = Yes
A[1,3] = No

In the above example, at A[0,2], the characters at the index values 0 to 2 are “aba” and equal to the substring B. So, the output is “Yes”.

At A[1, 3], the characters at the index values 1 to 3 are “bab” and are not equal to the substring B. Hence, the output is No.

HTTPie AI
HTTPie AI

AI API开发工具

下载

Implementation 2

A = “TutorialsPoint”
B = “Tutorials”
Queries[] = {0, 9, 14}

Output

A[0,9] = Yes
A[1, 10] = No
A[2, 11] = No

In the above example, we will check the existence of substring B in string A by using the query value as the index value of string A. At A[0, 9], substring B is present in string A and the output is Yes. After this at other index values B is not present in A so the output is No.

Example

To implement the above example in C++ programming language we use the Rolling Hash algorithm to match the substring with the input string. Calculate the hash values of substring B using the hash table. The hash table provides key-value pairs.Use the rolling hash algorithm for faster and avoid rehashing of string A.

#include 
#define mod 3803
#define d 26
using namespace std;
 
int hash_y;
int* hash_x;
int* pro;
 
//user defined function to calculate the hash values
int modin(int z){
   int q = mod - 2;
   int r = 1;
   while (q != 1) {
      if (q % 2 == 1)
         r = (r * z) % mod;
      z = (z * z) % mod;
      q /= 2;
   }
   return (r * z) % mod;
}
 
// Function to generate hash
void getOut(string& x, string& y){ 
   hash_x = new int[x.size()];
   pro = new int[x.size()];
   for (int j = y.size() - 1; j >= 0; j--)
      hash_y = (hash_y * d + (y[j] - 97)) % mod;
 
   pro[0] = 1;
   hash_x[0] = (x[0] - 97) % mod;
   for (int j = 1; j < x.size(); j++) {
      pro[j] = (pro[j - 1] * d) % mod;
      hash_x[j] = (hash_x[j - 1] + pro[j] * (x[j] - 97)) % mod;
   }
}
//user defined function to return check substring is present in string or not
bool checkEq(int j, int len_x, int len_y){
   int z;
   
   if (j == 0)
      z = hash_x[len_y - 1];
   else {
      z = (hash_x[j + len_y - 1] - hash_x[j - 1]
         + 2 * mod)
         % mod;
      z = (z * modin(pro[j])) % mod;
   }
 
   if (z == hash_y)
      return true;
   return false;
}
 
// Controller
int main(){
   string x = "TutorialsPoint";
   string y = "Tutorials";
   //calling function
   getOut(x, y);
    
   //calling queries function
   int queries[] = { 0, 9, 14};
   int q = sizeof(queries) / sizeof(queries[0]);
    
   for (int i = 0; i < q; i++){
      if (checkEq(queries[i], x.size(), y.size()))
         cout << "Yes substring is present\n";
      else
         cout << "No substring is not present\n";
   }
   return 0;
}

Output

Yes substring is present
No substring is not present
Yes substring is not present

结论

在本教程中,我们开发了C++代码来实现查找查询以检查子字符串是否存在于字符串中的任务。我们使用了滚动哈希方法来生成查询并获取结果。滚动哈希算法是一种在C++中计算子字符串哈希值的字符串算法,它使用旧值计算哈希值。为了使任务简单和简单,我们使用哈希函数计算哈希值。我们可以根据需要使用多个哈希函数。

相关专题

更多
string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

312

2023.08.02

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

711

2023.08.22

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

248

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

205

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1435

2023.10.24

字符串介绍
字符串介绍

字符串是一种数据类型,它可以是任何文本,包括字母、数字、符号等。字符串可以由不同的字符组成,例如空格、标点符号、数字等。在编程中,字符串通常用引号括起来,如单引号、双引号或反引号。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

609

2023.11.24

java读取文件转成字符串的方法
java读取文件转成字符串的方法

Java8引入了新的文件I/O API,使用java.nio.file.Files类读取文件内容更加方便。对于较旧版本的Java,可以使用java.io.FileReader和java.io.BufferedReader来读取文件。在这些方法中,你需要将文件路径替换为你的实际文件路径,并且可能需要处理可能的IOException异常。想了解更多java的相关内容,可以阅读本专题下面的文章。

547

2024.03.22

php中定义字符串的方式
php中定义字符串的方式

php中定义字符串的方式:单引号;双引号;heredoc语法等等。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

539

2024.04.29

小游戏4399大全
小游戏4399大全

4399小游戏免费秒玩大全来了!无需下载、即点即玩,涵盖动作、冒险、益智、射击、体育、双人等全品类热门小游戏。经典如《黄金矿工》《森林冰火人》《狂扁小朋友》一应俱全,每日更新最新H5游戏,支持电脑与手机跨端畅玩。访问4399小游戏中心,重温童年回忆,畅享轻松娱乐时光!官方入口安全绿色,无插件、无广告干扰,打开即玩,快乐秒达!

30

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

MySQL 初学入门(mosh老师)
MySQL 初学入门(mosh老师)

共3课时 | 0.3万人学习

微信小程序开发之API篇
微信小程序开发之API篇

共15课时 | 1.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号