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

查询字符串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] = YesA[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.

Implementation 2

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

Output

A[0,9] = YesA[1, 10] = NoA[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 26using namespace std; int hash_y;int* hash_x;int* pro; //user defined function to calculate the hash valuesint 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 hashvoid 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 notbool 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;} // Controllerint 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 presentn";      else         cout << "No substring is not presentn";   }   return 0;}

Output

Yes substring is presentNo substring is not presentYes substring is not present

结论

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

以上就是查询字符串A中是否存在字符串B作为子字符串的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444317.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:21:36
下一篇 2025年12月17日 21:21:49

相关推荐

  • 如何使用JavaScript在不同HTML页面间传递CSS样式值

    本文详细探讨了如何在不同html页面之间传递并持久化css样式值,以实现ui状态的同步。文章首先分析了直接存储dom元素对象导致的问题,随后提出了使用javascript和`localstorage`存储和检索css属性值或样式类名的有效方法。通过提供清晰的代码示例和最佳实践,本教程旨在帮助开发者理…

    2025年12月23日
    000
  • PHP 多语言网站切换:会话管理与翻译函数实践

    本教程详细介绍了使用 php 构建多语言网站的实现方法。文章涵盖了如何通过 url 参数和会话管理实现语言切换,以及如何设计一套健壮的翻译加载与显示机制,以避免常见的变量未定义和字符串偏移错误。通过封装的辅助函数,确保翻译内容正确加载和渲染,提升代码的可维护性和用户体验。 构建多语言网站的核心挑战 …

    2025年12月23日
    300
  • 使用JavaScript和Fetch API动态渲染新闻列表:解决内容覆盖问题

    本教程详细讲解如何利用javascript的fetch api从restful接口获取数据,并动态生成html内容以在网页上展示新闻标题列表。文章将深入探讨在处理数组数据时,如何避免在循环中错误地覆盖dom内容,确保所有数据项都能被正确渲染,从而解决api数据动态渲染时常见的只显示最后一项的问题。 …

    2025年12月23日 好文分享
    000
  • 如何删除html节点_HTML DOM节点删除(removeChild)与内存释放方法

    删除HTML节点的核心方法是removeChild,需通过父节点移除子节点,如parentNode.removeChild(childNode)。现代浏览器也支持更便捷的element.remove()方法,允许节点直接删除自身。节点被移除后虽脱离DOM树,但若JavaScript仍持有其引用(如事…

    2025年12月23日
    100
  • 动态加载图片到Swiper轮播图的正确姿势

    本文详细介绍了如何使用javascript动态加载图片数据并正确填充到swiper轮播图的每个幻灯片中。通过分析常见的错误,如误用`queryselector`和不当的dom操作,教程演示了如何利用`queryselectorall`和适当的迭代方法,确保每张图片都能准确地显示在对应的轮播幻灯片中,…

    好文分享 2025年12月23日
    000
  • R语言:从HTML页面高效提取并解析内嵌JSON数据

    本教程详细介绍了在R语言中如何从包含JSON数据的HTML网页中提取并解析所需信息。针对rvest无法直接解析内嵌JSON的问题,我们将展示如何利用html_text()获取网页的原始文本内容,再结合jsonlite::parse_json()将其转换为R数据结构,并进一步处理以提取特定嵌套字段,最…

    2025年12月22日
    000
  • HTML表格排序怎么实现_HTML表格JavaScript排序功能教程

    通过JavaScript实现HTML表格排序,核心是监听表头点击事件,获取列数据后按类型(字符串、数字、日期)进行升序或降序排序,并利用DocumentFragment优化DOM操作以提升性能。 HTML表格排序,说白了就是通过JavaScript来动态调整表格行的顺序。这听起来可能有点复杂,但核心…

    2025年12月22日 好文分享
    000
  • 动态创建HTML输入字段、捕获其值并构建动态字符串的JavaScript教程

    本文详细介绍了如何使用JavaScript动态创建HTML输入字段,高效捕获这些动态字段的用户输入值,并利用这些值灵活构建动态字符串。教程将指导读者避免传统变量命名陷阱,转而采用数组和类选择器等现代方法,确保代码的健壮性和可扩展性。 在现代web应用开发中,经常需要根据用户交互动态地添加或移除表单元…

    2025年12月22日
    000
  • 如何在JavaScript中将字符串转换为小写字母?

    要将 JavaScript 中的字符串转换为小写字母,请使用 toLocaleLowerCase() 方法。 示例 您可以尝试运行以下代码来了解如何在 JavaScript 中使用 toLocaleLowerCase() 方法 – 实时演示 var a = “WELCOME!”; doc…

    2025年12月21日
    000
  • 匹配任何包含零个或多个p的字符串

    要使用 JavaScript RegExp 匹配任何包含零个或多个 p 的字符串,请使用 p* 量词。 示例 您可以尝试运行以下代码来匹配包含零个或多个 p 的任何字符串。这里,p 被认为是出现的次数 – JavaScript Regular Expression var myStr =…

    2025年12月21日
    000
  • 如何将当前语言环境的约定,将日期的“时间”部分作为字符串返回?

    要使用当前语言环境的约定以字符串形式返回日期的“时间”部分,请使用 toLocaleTimeString() 方法。 toLocaleTimeString 方法依赖于格式化日期的底层操作系统。它使用运行脚本的操作系统的格式约定将日期转换为字符串。例如,在美国,月份出现在日期之前 (04/15/98)…

    2025年12月21日
    000
  • JavaScript数组-字符串-数学函数

    这次给大家带来javascript数组-字符串-数学函数,使用javascript数组-字符串-数学函数的注意事项有哪些,下面就是实战案例,一起来看一下。 数组方法里push、pop、shift、unshift、join、split分别是什么作用。push()方法添加一个或多个元素到数组的末尾,并返…

    好文分享 2025年12月21日
    000
  • js中repeat()的使用

    repeat()方法用于将字符串重复指定次数并返回新字符串。例如’Hello’.repeat(3)结果为’HelloHelloHello’;传入小数自动向下取整,负数或无法转换的字符串会报错。 在 JavaScript 中,repeat() 是一个字符…

    2025年12月21日
    000
  • JavaScript输入框数值计算结果的货币格式化显示

    本教程详细介绍了如何利用javascript的`tolocalestring()`方法,将用户输入框中的数值计算结果,如乘积,优雅地格式化为符合特定区域设置的货币形式(例如$9,000),并将其显示在另一个输入框中。文章将涵盖`tolocalestring()`的关键参数及其在实际应用中的配置,帮助…

    2025年12月21日
    000
  • 如何从HTML字符串中高效提取标签的src属性

    <img src="https://img.php.cn/upload/article/001/246/273/175902558447559.jpg" alt="如何从HTML字符串中高效提取标签的src属性”>标签的src属性” …

    好文分享 2025年12月20日
    000
  • js怎么判断字符串是否包含子串

    判断字符串是否包含子串最推荐使用includes(),因其语义清晰且直接返回布尔值;2. 若需获取子串位置或兼容旧浏览器,则选用indexof(),通过返回值是否为-1判断存在性;3. 对于复杂模式匹配或不区分大小写的查找,应使用正则表达式,其中test()方法适合布尔判断,match()可返回匹配…

    2025年12月20日
    000
  • js如何将字符串转换为数组

    在javascript中,将字符串转换为数组的核心方法是使用split()。1. 使用split()可根据指定分隔符将字符串分割成数组,如str.split(“,”)可按逗号分割;2. 当存在连续分隔符时,split()会保留空字符串元素,可通过filter(boolean)…

    2025年12月20日 好文分享
    000
  • javascript如何将数组转为字符串

    javascript中将数组转换为字符串最直接的方法是使用join()或tostring();2. join()方法可自定义分隔符,若不指定则默认使用逗号,而tostring()方法始终使用逗号且不接受参数;3. join()适用于需要控制输出格式的场景,如生成csv、url参数或html内容,to…

    2025年12月20日 好文分享
    000
  • js如何实现字符串截断

    javascript中截断字符串的核心方法有slice()、substring()和substr();2. slice()支持负索引且行为可预测,推荐使用;3. substring()不支持负索引且会自动交换参数顺序,需注意其“智能”行为;4. substr()第二个参数为长度而非结束索引,但已被废…

    2025年12月20日
    000
  • js如何判断字符串包含子串 字符串包含检测的3种实用技巧

    判断javascript字符串是否包含子串,主要有三种方法:1.includes() 方法最直观且推荐,返回布尔值表示是否包含指定子串;2.indexof() 方法通过返回索引或 -1 判断是否包含,需额外比较操作;3.正则表达式 test() 方法更灵活,支持复杂模式匹配。选择依据具体需求:简单查…

    2025年12月20日 好文分享
    100

发表回复

登录后才能评论
关注微信