图论 弦_混乱的弦

图论 弦

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.mzph.cn/news/540541.shtml

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

相关文章

「原创」从马云、马化腾、李彦宏的对话,看出三人智慧差在哪里?

在今年中国IT领袖峰会上&#xff0c;马云、马化腾、李彦宏第一次单独合影&#xff0c;同框画面可以说很难得了。BAT关心的走势一直是同行们竞相捕捉的热点&#xff0c;所以三位大Boss在这次大会上关于人工智能的见解&#xff0c;也受到广泛关注与多方解读。马云认为机器比人聪明…

字符串矩阵转换成长字符串_字符串矩阵

字符串矩阵转换成长字符串Description: 描述&#xff1a; In this article, we are going to see how backtracking can be used to solve following problems? 在本文中&#xff0c;我们将看到如何使用回溯来解决以下问题&#xff1f; Problem statement: 问题陈述&#xf…

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类用于…

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…

构建安全网络 比格云全系云产品30天内5折购

一年之计在于春&#xff0c;每年的三、四月&#xff0c;都是个人创业最佳的起步阶段&#xff0c;也是企业采购最火热的时期。为了降低用户的上云成本&#xff0c;让大家能无门槛享受到优质高性能的云服务&#xff0c;比格云从3月16日起&#xff0c;将上线“充值30天内&#xff…

数据结构 基础知识

一。逻辑结构: 是指数据对象中数据 素之间的相互关系。 其实这也是我 今后最需要关注的问题 逻辑结构分为以 四种1. 集合结构 2.线性结构 3.数形结构 4&#xff0c;图形结构 二。物理结构&#xff1a; 1&#xff0c;顺序存储结,2 2. 链式存储结构 一&#xff0c;时间复杂…

ruby 变量类中范围_Ruby中的类

ruby 变量类中范围Ruby类 (Ruby Classes) In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object o…

以云计算的名义 驻云科技牵手阿里云

本文讲的是以云计算的名义 驻云科技牵手阿里云一次三个公司的牵手 可能会改变无数企业的命运 2017年4月17日&#xff0c;对于很多人来说可能只是个平常的工作日&#xff0c;但是对于国内无数的企业来说却可能是个会改变企业命运的日。驻云科技联合国内云服务提供商阿里云及国外…

浏览器端已支持 ES6 规范(包括 export import)

当然&#xff0c;是几个比较优秀的浏览器&#xff0c;既然是优秀的浏览器&#xff0c;大家肯定知道是那几款啦&#xff0c;我就不列举了&#xff0c;我用的是 chrome。 对 script 声明 type 为 module 后就可以享受 es6 规范所带来的模块快感了。 基础语法既然是全支持&#xf…

一文读懂深度学习框架下的目标检测(附数据集)

从简单的图像分类到3D位置估算&#xff0c;在机器视觉领域里从来都不乏有趣的问题。其中我们最感兴趣的问题之一就是目标检测。 如同其他的机器视觉问题一样&#xff0c;目标检测目前为止还没有公认最好的解决方法。在了解目标检测之前&#xff0c;让我们先快速地了解一下这个领…

设计一个应用程序,以在C#中的按钮单击事件上在MessageBox中显示TextBox中的文本...

Here, we took two controls on windows form that are TextBox and Button, named txtInput and btnShow respectively. We have to write C# code to display TextBox’s text in the MessageBox on Button Click. 在这里&#xff0c;我们在Windows窗体上使用了两个控件&…

Oracle优化器:星型转换(Star Query Transformation )

Oracle优化器&#xff1a;星型转换&#xff08;Star Query Transformation &#xff09;Star query是一个事实表&#xff08;fact table&#xff09;和一些维度表&#xff08;dimension&#xff09;的join。每个维度表都跟事实表通过主外键join&#xff0c;且每个维度表之间不j…

JavaScript | 声明数组并使用数组索引分配元素的代码

Declare an array, assign elements by indexes and print all elements in JavaScript. 声明一个数组&#xff0c;通过索引分配元素&#xff0c;并打印JavaScript中的所有元素。 Code: 码&#xff1a; <html><head><script>var fruits [];fruits[0]"…

Kubernetes基础组件概述

本文讲的是Kubernetes基础组件概述【编者的话】最近总有同学问Kubernetes中的各个组件的相关问题&#xff0c;其实这些概念内容在官方文档中都有&#xff0c;奈何我们有些同学可能英文不好&#xff0c;又或者懒得去看&#xff0c;又或者没有找到&#xff0c;今天有时间就专门写…

c语言将链表写入二进制文件_通过逐级遍历将二进制树转换为单链表的C程序

c语言将链表写入二进制文件Problem statement: Write a C program to convert a binary tree into a single linked list by traversing level-wise. 问题陈述&#xff1a;编写一个C程序&#xff0c;通过逐级遍历将二进制树转换为单个链表 。 Example: 例&#xff1a; The ab…

洛谷 P2689 东南西北【模拟/搜索】

题目描述 给出起点和终点的坐标及接下来T个时刻的风向(东南西北)&#xff0c;每次可以选择顺风偏移1个单位或者停在原地。求到达终点的最少时间。 如果无法偏移至终点&#xff0c;输出“-1”。 输入输出格式 输入格式&#xff1a; 第一行两个正整数x1,y1&#xff0c;表示小明所…

单链表遍历_单链表及其遍历实现的基本操作

单链表遍历单链表 (Single linked list) Single linked list contains a number of nodes where each node has a data field and a pointer to next node. The link of the last node is to NULL, indicates end of list. 单个链表包含许多节点&#xff0c;其中每个节点都有一…

8086简单的指令流水线_在8086微处理器中执行流水线的指令和概念的步骤

8086简单的指令流水线Any computer or machine works according to some instructions. These instructions are responsible for all the work that the machine does. But how does a machine work to understand and execute that instruction? 任何计算机或机器都按照某些…

node.js 爬虫入门总结

node.js爬虫 前端同学可能向来对爬虫不是很感冒&#xff0c;觉得爬虫需要用偏后端的语言&#xff0c;诸如 php &#xff0c; python 等。当然这是在 nodejs 前了&#xff0c;nodejs 的出现&#xff0c;使得 Javascript 也可以用来写爬虫了。由于 nodejs 强大的异步特性&#xf…