字符串查找字符出现次数_查找字符串作为子序列出现的次数

字符串查找字符出现次数

Description:

描述:

It's a popular interview question based of dynamic programming which has been already featured in Accolite, Amazon.

这是一个流行的基于动态编程的面试问题,已经在亚马逊的Accolite中得到了体现。

Problem statement:

问题陈述:

Given two strings S and T, find the number of times the second string occurs in the first string, whether continuous or discontinuous as subsequence.

给定两个字符串ST ,找出第二个字符串在第一个字符串中出现的次数,无论是连续的还是不连续的作为子序列。

    Input:
String S: "iloveincludehelp"
String T: "il"
Output:
5

Explanation:

说明:

The first string is,

第一个字符串是

Find number of times a string occurs as a subsequence (1)

The second string is "il"

第二个字符串是“ il”

First occurrence:

第一次出现:

Find number of times a string occurs as a subsequence (2)

Second occurrence:

第二次出现:

Find number of times a string occurs as a subsequence (2)

Third occurrence:

第三次出现:

Find number of times a string occurs as a subsequence (2)

Fouth occurrence:

发生口:

Find number of times a string occurs as a subsequence (2)

Fifth occurrence:

第五次出现:

Find number of times a string occurs as a subsequence (2)

So, total distinct occurrences are 5.

因此,总的不重复发生次数为5。

Solution Approach:

解决方法:

First, we discuss the recursive solution and then we will convert it to dynamic programming.

首先,我们讨论递归解决方案,然后将其转换为动态编程。

Prerequisite:

先决条件:

    string s: the first string
string t: the second string
starts: start point of the first string
srartt: start point of the second string
m : length of first string
n : length of second string

How, how can we generate a recursive relation?

如何,如何生成递归关系?

Say,

说,

    starts=i where i<m and i>=0 & start=j where j<n and j>=0

Say,

说,

  1. s[starts] = t[start] that means both have same character,

    s [starts] = t [start]表示两个字符相同,

    Now we have to option,

    现在我们必须选择

    1. Check for starts+1, startt+1 which means we are looking for the same occurrence, we want to check for other characters as well.
    2. 检查starts + 1 , startt + 1 ,这意味着我们正在寻找相同的事件,我们也想检查其他字符。
    3. Check for starts+1, startt which means we are looking for another different occurrence.
    4. 检查starts + 1和startt ,这意味着我们正在寻找另一个不同的事件。
  2. s[starts] != t[start]

    s [开始]!= t [开始]

    Now we have only one option which is check for

    现在我们只有一个选项可以检查

    starts+1, startt as we need to look for different occurrence only.

    starts + 1 , startt,因为我们只需要查找不同的事件。

    Function distinctOccurence(string s,string t,int starts,int startt,int m,int n)
if startt==n   //enter substring is matched
return 1;
if starts==m         //enter string has been searched with out match 
return 0;
if(s[starts]!=t[startt])
//only one option as we discussed
return distinctOccurence(s,t,starts+1,startt,m,n); 
else
//both the options as we discussed
return distinctOccurence(s,t,starts+1,startt+1,m,n) + distinctOccurence(s,t,starts+1,startt,m,n);

The above recursion will generate many overlapping subproblems and hence we need to use dynamic programming. (I would recommend to take two short string and try doing by your hand and draw the recursion tree to understand how recursion is working).

上面的递归将产生许多重叠的子问题,因此我们需要使用动态编程。 (我建议您取两个短字符串,然后用手尝试画出递归树,以了解递归的工作方式)。

Let's convert the recursion to DP.

让我们将递归转换为DP。

    Step1: initialize DP table
int dp[m+1][n+1];
Step2: convert step1 of recursive function
for i=0 to n
dp[0][i]=0;
Step3: convert step2 of recursive function
for i=0 to m
dp[i][0]=1;
Step4:   Fill the DP table which is similar to step3 of the recursion function
for i=1 to m
for j=1 to n
if s[i-1]==t[j-1]
dp[i][j]=dp[i-1][j]+dp[i-1][j-1]
else
dp[i][j]=dp[i-1][j]
end for
end for
Step5: return dp[m][n] which is the result.

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int distinctOccurence(string s, string t, int starts, int startt, int m, int n) {
//note argument k,l  are of no use here
//initialize dp table
int dp[m + 1][n + 1];
//base cases
for (int i = 0; i <= n; i++)
dp[0][i] = 0;
for (int i = 0; i <= m; i++)
dp[i][0] = 1;
//fill the dp table
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s[i - 1] == t[j - 1])
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[m][n];
}
int main() {
int n, m;
string s1, s2;
cout << "Enter the main string:\n";
cin >> s1;
cout << "Enter the substring:\n";
cin >> s2;
m = s1.length();
n = s2.length();
cout << s2 << " has " << distinctOccurence(s1, s2, 0, 0, m, n) << " times different occurences in " << s1 << endl;
return 0;
}

Output

输出量

Enter the main string:
iloveincludehelp
Enter the substring:
il
il has 5 times different occurences in iloveincludehelp

翻译自: https://www.includehelp.com/icp/find-number-of-times-a-string-occurs-as-a-subsequence.aspx

字符串查找字符出现次数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542262.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu 忘记密码的处理方法

Ubuntu系统启动时选择recovery mode&#xff0c;也就是恢复模式。接着选择Drop to root shell prompt ,也就是获取root权限。输入命令查看用户名 cat /etc/shadow &#xff0c;$号前面的是用户名输入命令&#xff1a;passwd "用户名" 回车就可以输入新密码了转载于:…

服务器mdl文件转换,Simulink Project 中 MDL 到 SLX 模型文件格式的转换

打开弹体示例项目并将 MDL 文件另存为 SLX运行以下命令以创建并打开“sldemo_slproject_airframe”示例的工作副本。Simulink.ModelManagement.Project.projectDemo(airframe, svn);rebuild_s_functions(no_progress_dialog);Creating sandbox for project.Created example fil…

vue 修改div宽度_Vue 组件通信方式及其应用场景总结(1.5W字)

前言相信实际项目中用过vue的同学&#xff0c;一定对vue中父子组件之间的通信并不陌生&#xff0c;vue中采用良好的数据通讯方式&#xff0c;避免组件通信带来的困扰。今天笔者和大家一起分享vue父子组件之间的通信方式&#xff0c;优缺点&#xff0c;及其实际工作中的应用场景…

Java System类identityHashCode()方法及示例

系统类identityHashCode()方法 (System class identityHashCode() method) identityHashCode() method is available in java.lang package. identityHashCode()方法在java.lang包中可用。 identityHashCode() method is used to return the hashcode of the given object – B…

Linux中SysRq的使用(魔术键)

转&#xff1a;http://www.chinaunix.net/old_jh/4/902287.html 魔术键&#xff1a;Linux Magic System Request Key Hacks 当Linux 系统不能正常响应用户请求时, 可以使用SysRq小工具控制Linux. 一 SysRq的启用与关闭 要想启用SysRq, 需要在配置内核时设置Magic SysRq key (CO…

链接服务器访问接口返回了消息没有活动事务,因为链接服务器 SQLEHR 的 OLE DB 访问接口 SQLNCLI10 无法启动分布式事务。...

查看一下MSDTC啟動是否正確1、运行 regedt32&#xff0c;浏览至 HKEY_LOCAL_MACHINE\Software\Microsoft\MSDTC。添加一个 DWORD 值 TurnOffRpcSecurity&#xff0c;值数据为 1。2、重启MS DTC服务。3、打开“管理工具”的“组件服务”。a. 浏览至"启动管理工具"。b.…

micropython 蜂鸣器_基于MicroPython的TPYBoard微信远程可燃气体报警器的设计与实现...

前言在我们平时的生活中&#xff0c;经常看到因气体泄漏发生爆炸事故的新闻。房屋起火、人体中毒等此类的新闻报道层出不穷。这种情况下&#xff0c;人民就发明了可燃气体报警器。当工业环境、日常生活环境(如使用天然气的厨房)中可燃性气体发生泄露&#xff0c;可燃气体报警器…

Java PropertyPermission getActions()方法与示例

PropertyPermission类的getActions()方法 (PropertyPermission Class getActions() method) getActions() method is available in java.util package. getActions()方法在java.util包中可用。 getActions() method is used to get the list of current actions in the form of…

源码安装nginx以及平滑升级

源码安装nginx以及平滑升级作者&#xff1a;尹正杰版权声明&#xff1a;原创作品&#xff0c;谢绝转载&#xff01;否则将追究法律责任。欢迎加入&#xff1a;高级运维工程师之路 598432640这个博客不方便上传软件包&#xff0c;我给大家把软件包放到百度云链接&#xff1a;htt…

ajax 跨站返回值,jquery ajax 跨域问题

补充回答&#xff1a;你的动态页只是一个请求页。例如你新建一个 get.asp 页面&#xff0c;用以下代码&#xff0c;在服务端实现像URL异步(ajax)请求&#xff0c;将请求结果输出。客户端页面再次用ajax(JS或者jquery的)向get.asp请求数据。两次ajax完成异域数据请求。get.asp代…

Bootstrap学习笔记系列1-------Bootstrap网格系统

目录 Bootstrap网格系统 学习笔记简单网格偏移列嵌套列列排序Bootstrap网格系统 学习笔记 简单网格 先上代码再解释 <!DOCTYPE html> <html><head><title>Bootstrap 模板</title><meta charset"utf-8"><!-- 引入 Bootstrap -…

Java类类的getDeclaringClass()方法和示例

类的类getDeclaringClass()方法 (Class class getDeclaringClass() method) getDeclaringClass() method is available in java.lang package. getDeclaringClass()方法在java.lang包中可用。 getDeclaringClass() method is used to return the declared Class object denotin…

乐高泰坦机器人视频解说_“安防”机器人将亮相服贸会

可巡视园区、自动避障、自动充电&#xff0c;实现24小时巡逻&#xff0c;与后台链接实时视频监控&#xff0c;异常检测……17日下午&#xff0c;北青-北京头条记者在特斯联科技集团有限公司的展厅中看到&#xff0c;一款“身怀绝技”的“安防”机器人备受关注。这款机器人也将在…

ios上传文件云服务器上,ios文件上传服务器

ios文件上传服务器 内容精选换一换在当前的迁移流程中&#xff0c;可能会存在迁移后ECS控制台镜像名称与实际操作系统不一致的现象。在当前机制下&#xff0c;该现象属于正常现象。该处显示的是下发ECS时使用的镜像名称&#xff0c;而不是操作系统名称。如果设置目的端时使用的…

这是一个UIImage集合类,可以很方便的对图片的染料(着色),增加亮度(闪电)和降低亮度(黑)和其他扩展的功能模块。...

2019独角兽企业重金招聘Python工程师标准>>> 这是一个UIImage集合类&#xff0c;可以很方便的对图片的染料&#xff08;着色&#xff09;&#xff0c;增加亮度&#xff08;闪电&#xff09;和降低亮度&#xff08;黑&#xff09;和其他扩展的功能模块。 在swift下实…

python爬取酷狗音乐top500_python获取酷狗音乐top500的下载地址 MP3格式

下面先给大家介绍下python获取酷狗音乐top500的下载地址 MP3格式&#xff0c;具体代码如下所示&#xff1a;# -*- coding: utf-8 -*-# Time : 2018/4/16# File : kugou_top500.py# Software: PyCharm# pyVer : python 2.7import requests,jsonheaders{UserAgent : Mozilla/5.0 …

微商相册一直显示服务器偷懒,【小程序】微商个人相册多端小程序源码以及安装...

程序介绍学习node.js顺便接的400元单子&#xff0c;前后端都是自己写&#xff0c;相比自己以前写的&#xff0c;这次相对来说比较规范&#xff0c;用于个人相册展示&#xff0c;适合微商&#xff0c;有客服联系&#xff0c;无需后台管理系统&#xff0c;小程序上直接进行管理&a…

stl优先队列定义可以吗_C ++ STL | 用户定义的优先级队列比较器

stl优先队列定义>可以吗In this article, we are going to see how to write your comparator function for priority queue in C STL using the lambda function. This is going to help you certainly to use priority queue more widely when you may have skipped think…

python编程求三角形面积公式_python编程 输入三角形的三条边,计算三角形的面积\...

展开全部# -*- coding: UTF-8 -*-# Filename : test.py# author by : www.runoob.coma float(input(输入三角62616964757a686964616fe59b9ee7ad9431333433633338形第一边长: ))b float(input(输入三角形第二边长: ))c float(input(输入三角形第三边长: ))# 计算半周长s (a …

ipfs分布式存储网络服务器系统,IPFS分布式存储是什么意思 分布式云存储服务器详解...

一直以来&#xff0c;数据的安全性&#xff0c;存储的隐私性都是用户很重视的方面。基于此&#xff0c;再加上现在媒体对于分布式存储的疯狂报道&#xff0c;分布式存储一词再度涌入了大家的视野之中&#xff0c;接下来IPFS新说就为大家详解一下有关IPFS分布式存储的知识。VIPF…