圖論 弦_混亂的弦

圖論 弦

Problem statement:

問題陳述:

You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the subsequence "includehelp" can be formed from the string S.

將為您提供輸入字符串S和字符串“ includehelp”。 您需要找出字符串S中所有可能的子序列“ includehelp”。 找出從字符串S形成子序列“ includehelp”的方式的數量。

Input:

輸入:

Input is the string s

輸入是字符串s

Output:

輸出:

Corresponding to each test case, print in a new line, a number denoting the number of ways in which we can form the subsequence "includehelp". Since output can be very large find the answer modulo 1000000007.

對應于每個測試用例,在新行中打印,該數字表示我們形成子序列“ includehelp”的方式的數量。 由于輸出可能非常大,因此請找到模1000000007的答案。

Constraints:

限制條件:

Input String contains only lowercase English Letters and string length is 5000 at maximum.

輸入字符串僅包含小寫英文字母,并且字符串長度最大為5000。

Example:

例:

Input:
includehelp
Output:
1
Explanation:
There is only one instances of "includehelp" 
in the above input string.
Input:
iincludehelp
Output:
2
Explanation:
There is two instances of "includehelp" 
in the above input string.

jumbled strings

Solution Approach:

解決方法:

Let the input string be, s and t="includehelp"
This problem can be solved by recursion.
So we have,
string s: the input string
string t: the second string ("includehelp")
starts  : start point of string s
srartt  : start point of string t, ("includehelp")
m: length of input string
11: length of string t,"includehelp"
MOD: 1000000009

Now, how can we generate a recursive relation?

現在,我們如何生成遞歸關系?

Say starts=i where 0<=i<m & startt=j where 0<=j<11

starts = i ,其中0 <= i <m&startt = j ,其中0 <= j <11

Say,

說,

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

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

    Now we have two options,

    現在我們有兩個選擇,

    1. starts+1, starts + 1startt+1 which means we are looking for the same occurrence, we want to check for other characters as well.startt + 1 ,這意味著我們正在尋找相同的事件,我們也想檢查其他字符。
    2. starts+1, starts + 1startt which means we are looking for another different occurrence.startt ,這意味著我們正在尋找另一個不同的事件。
  2. s[starts]!=t[startt]

    s [starts]!= t [startt]

    Now we have only one option which is check for

    現在我們只有一個選項可以檢查

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

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

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

The above recursion will generate many overlapping subproblems and hence we need to use dynamic programming.

上面的遞歸將產生許多重疊的子問題,因此我們需要使用動態編程。

Let's convert the recursion to DP.

讓我們將遞歸轉換為DP。

  • Step 1: initialize DP table,

    步驟1:初始化DP表,

    int dp[m+1][12];
    
    
  • Step 2: convert step1 of recursive function,

    步驟2:轉換遞歸函數的step1,

    for i=0 to 11
    dp[0][i]=0;
    
    
  • Step 3: convert step 2 of recursive function,

    步驟3:轉換遞歸函數的步驟2,

    for i=0 to m
    dp[i][0]=1;
    
    
  • Step 4: Fill the DP table which is similar to step3 of the recursion function,

    步驟4:填寫DP表,該表類似于遞歸函數的步驟3,

    for i=1 to m
    for j=1 to 11
    if s[i-1]==t[j-1]
    dp[i][j]=(dp[i-1][j]+dp[i-1][j-1])%MOD
    else
    dp[i][j]=dp[i-1][j]
    end for
    end for
    
    
  • Step5: return dp[m][11] which is the result.

    步驟5:返回結果dp [m] [11]

The above DP technique is known as the tabulation process. We can introduce memorization as well, known as the top-down approach. Where we store every computed subproblem and while computing first we look up our DP table whether sub-problem is already solved or not. Check the below top-down implementation for the above problem.

上述DP技術稱為制表過程。 我們也可以引入記憶,稱為自頂向下方法。 我們存儲每個計算出的子問題的位置,并且在進行第一次計算時,無論子問題是否已解決,我們都會查看DP表。 檢查以下自上而下的實現是否存在上述問題。

C++ Implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
int dp[1001][12];
int jumbled(string s1, string s2, int i, int j, int n, int m)
{
if (j == m) {
return 1;
}
if (i == n && j != m)
return 0;
// if subproblem already solved
if (dp[i][j] != -1) 
return dp[i][j];
if (s1[i] == s2[j]) {
dp[i][j] = jumbled(s1, s2, i + 1, j + 1, n, m) + jumbled(s1, s2, i + 1, j, n, m);
}
else {
dp[i][j] = jumbled(s1, s2, i + 1, j, n, m);
}
return dp[i][j];
}
int main()
{
int n;
string s2 = "includehelp";
string s1;
cout << "Input string: ";
cin >> s1;
n = s1.length();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 11; j++)
dp[i][j] = -1;
}
cout << "We can jumble " << jumbled(s1, s2, 0, 0, n, 11) << " of ways." << endl;
return 0;
}

Output:

輸出:

Input string: iincludehelp
We can jumble 2 of ways.

翻譯自: https://www.includehelp.com/icp/jumbled-strings.aspx

圖論 弦

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540541.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540541.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540541.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[轉載] Python列表操作

參考鏈接&#xff1a; Python中的基本運算符 Python列表&#xff1a; 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置&#xff0c;或索引&#xff0c;第一個索引是0&#xff0c;第二個索引是1&#xff0c;依此類推&#xff1b; Python有6個序列的…

「原創」從馬云、馬化騰、李彥宏的對話,看出三人智慧差在哪里?

在今年中國IT領袖峰會上&#xff0c;馬云、馬化騰、李彥宏第一次單獨合影&#xff0c;同框畫面可以說很難得了。BAT關心的走勢一直是同行們競相捕捉的熱點&#xff0c;所以三位大Boss在這次大會上關于人工智能的見解&#xff0c;也受到廣泛關注與多方解讀。馬云認為機器比人聰明…

python 注釋含注釋_Python注釋

python 注釋含注釋Python注釋 (Python comments) Comments in Python are used to improve the readability of the code. It is useful information given by the programmer in source code for a better understanding of code and logic that they have used to solve the …

C2的完整形式是什么?

C2&#xff1a;核心2 (C2: Core 2) C2 is an abbreviation of "Core 2" or "Intel Core 2". C2是“ Core 2”或“ Intel Core 2”的縮寫 。 It is a family of Intels processor which was launched on the 27th of July, 2006. It comprises a series of…

scala特性_Scala | 特性應用

scala特性特性應用 (Trait App) Scala uses a trait called "App" which is used to convert objects into feasible programs. This conversion is done using the DelayedInit and the objects are inheriting the trait named App will be using this function. T…

[轉載] Python3中的表達式運算符

參考鏈接&#xff1a; Python中的除法運算符 1&#xff1a;Python常用表達式運算符 yield 生成器函數send協議 lambda args:expression 創建匿名函數 x if y else z 三元選擇表達式(當y為真時&#xff0c;x才會被計算) x or y 邏輯或(僅但x為假時y才會被計算) x and …

字符串矩陣轉換成長字符串_字符串矩陣

字符串矩陣轉換成長字符串Description: 描述&#xff1a; In this article, we are going to see how backtracking can be used to solve following problems? 在本文中&#xff0c;我們將看到如何使用回溯來解決以下問題&#xff1f; Problem statement: 問題陳述&#xf…

pythonchallenge_level2

level2 地址&#xff1a;http://www.pythonchallenge.com/pc/def/ocr.html。 源碼&#xff1a;gitcode.aliyun.com:qianlizhixing12/PythonChallenge.git。 問題&#xff1a;找出頁面源碼一點提示注釋中的稀有字符。 #!/usr/bin/env python3 # -*- coding:UTF-8 -*-# Level 2im…

[轉載] python類運算符的重載

參考鏈接&#xff1a; Python中的運算符重載 alist input().split() blist input().split() n float(input()) class Vector: def __init__(self, x0, y0, z0): # 請在此編寫你的代碼(可刪除pass語句) self.X x self.Y y self.Z z # 代碼結束 def __add__(self, other):…

r語言 運算符_R語言運算符

r語言 運算符R語言中的運算符 (Operators in R Language) Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical cal…

[轉載] Python基礎之類型轉換與算術運算符

參考鏈接&#xff1a; Python中的運算符函數| 1 一、注釋 1.注釋&#xff1a;對程序進行標注和說明&#xff0c;增加程序的可讀性。程序運行的時候會自動忽略注釋。 2.單行注釋&#xff1a;使用#的形式。但是#的形式只能注釋一行&#xff0c;如果有多行&#xff0c;就不方便…

java awt 按鈕響應_Java AWT按鈕

java awt 按鈕響應The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface. Button類用于…

解決“由于應用程序的配置不正確,應用程序未能啟動,重新安裝應用程序可能會糾正這個問題”...

在VS2005下用C寫的程序&#xff0c;在一臺未安裝VS2005的系統上&#xff0c; 用命令行方式運行&#xff0c;提示&#xff1a; “系統無法執行指定的程序” 直接雙擊運行&#xff0c;提示&#xff1a; “由于應用程序的配置不正確&#xff0c;應用程序未能啟動&#xff0c;重新安…

qgis在地圖上畫導航線_在Laravel中的航線

qgis在地圖上畫導航線For further process we need to know something about it, 為了進一步處理&#xff0c;我們需要了解一些有關它的信息&#xff0c; The route is a core part in Laravel because it maps the controller for sending a request which is automatically …

Logistic回歸和SVM的異同

這個問題在最近面試的時候被問了幾次&#xff0c;讓談一下Logistic回歸&#xff08;以下簡稱LR&#xff09;和SVM的異同。由于之前沒有對比分析過&#xff0c;而且不知道從哪個角度去分析&#xff0c;一時語塞&#xff0c;只能不知為不知。 現在對這二者做一個對比分析&#xf…

[轉載] python學習筆記2--操作符,數據類型和內置功能

參考鏈接&#xff1a; Python中的Inplace運算符| 1(iadd()&#xff0c;isub()&#xff0c;iconcat()…) 什么是操作符&#xff1f; 簡單的回答可以使用表達式4 5等于9&#xff0c;在這里4和5被稱為操作數&#xff0c;被稱為操符。 Python語言支持操作者有以下幾種類型。 算…

scala bitset_Scala中的BitSet

scala bitsetScala BitSet (Scala BitSet) Set is a collection of unique elements. 集合是唯一元素的集合。 Bitset is a set of positive integers represented as a 64-bit word. 位集是一組表示為64位字的正整數。 Syntax: 句法&#xff1a; var bitset : Bitset Bits…

構建安全網絡 比格云全系云產品30天內5折購

一年之計在于春&#xff0c;每年的三、四月&#xff0c;都是個人創業最佳的起步階段&#xff0c;也是企業采購最火熱的時期。為了降低用戶的上云成本&#xff0c;讓大家能無門檻享受到優質高性能的云服務&#xff0c;比格云從3月16日起&#xff0c;將上線“充值30天內&#xff…