字符串查找字符出現次數_查找字符串作為子序列出現的次數

字符串查找字符出現次數

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.pswp.cn/news/542262.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542262.shtml
英文地址,請注明出處:http://en.pswp.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…