日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串查找字符出现次数_查找字符串作为子序列出现的次数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串查找字符出現次數

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,

第一個字符串是

The second string is "il"

第二個字符串是“ il”

First occurrence:

第一次出現:

Second occurrence:

第二次出現:

Third occurrence:

第三次出現:

Fouth occurrence:

發生口:

Fifth occurrence:

第五次出現:

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 stringstring t: the second stringstarts: start point of the first stringsrartt: start point of the second stringm : length of first stringn : 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,

說,

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

    s [starts] = t [start]表示兩個字符相同,

    Now we have to option,

    現在我們必須選擇

  • Check for starts+1, startt+1 which means we are looking for the same occurrence, we want to check for other characters as well.
  • 檢查starts + 1 , startt + 1 ,這意味著我們正在尋找相同的事件,我們也想檢查其他字符。
  • Check for starts+1, startt which means we are looking for another different occurrence.
  • 檢查starts + 1和startt ,這意味著我們正在尋找另一個不同的事件。
  • 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 matchedreturn 1;if starts==m //enter string has been searched with out match return 0;if(s[starts]!=t[startt])//only one option as we discussedreturn distinctOccurence(s,t,starts+1,startt,m,n); else//both the options as we discussedreturn 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 tableint dp[m+1][n+1];Step2: convert step1 of recursive functionfor i=0 to ndp[0][i]=0;Step3: convert step2 of recursive functionfor i=0 to mdp[i][0]=1;Step4: Fill the DP table which is similar to step3 of the recursion functionfor i=1 to mfor j=1 to nif s[i-1]==t[j-1]dp[i][j]=dp[i-1][j]+dp[i-1][j-1]elsedp[i][j]=dp[i-1][j]end forend forStep5: 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 tableint dp[m + 1][n + 1];//base casesfor (int i = 0; i <= n; i++)dp[0][i] = 0;for (int i = 0; i <= m; i++)dp[i][0] = 1;//fill the dp tablefor (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];elsedp[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

    字符串查找字符出現次數

    總結

    以上是生活随笔為你收集整理的字符串查找字符出现次数_查找字符串作为子序列出现的次数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。