跟随者数字解码_跟随模式的数字

跟随者数字解码

Problem statement:

问题陈述:

Given a pattern containing only I's and D's. 'I' stands for increasing and 'D' for decreasing. Devise an algorithm to print the minimum number following that pattern. Digits are from 1-9 and digits can't repeat.

给定一个仅包含ID的模式“ I”代表增加, “ D”代表减少。 设计一种算法以按照该模式打印最小数量。 数字为1-9 ,数字不能重复。

Example:

例:

    Input:
IIDDIDD  
Output:
12543876

Solution

The pattern & number to be generated

生成的图案和编号

  1. Length of minimum number = string length+1

    最小数目 L的ength =字符串长度+ 1

    Hence, maximum string length possible is 8, since we can construct only with different digits (1-9)

    因此,最大字符串长度为8,因为我们只能用不同的数字(1-9)进行构造

  2. 'I' means the next digit will be 1 greater than the current & 'D' means the next digit will be 1 less than the current digit

    “ I”表示下一个数字比当前数字大1, “ D”表示下一个数字比当前数字小1。

    "II" → 123

    “ II”→123

    "DD" → 321

    “ DD”→321

The problem can be used with help of stack. The concept is to create stack with consecutive number same as depth of a local contiguous sequence of 'D'.

该问题可以在堆栈的帮助下使用。 概念是创建具有与本地连续序列“ D”的深度相同的连续数字的堆栈。

Prerequisite:

先决条件:

  1. Input pattern, string s

    输入模式,字符串s

  2. Stack st to store the digits

    堆叠st以存储数字

    Function findMinFromPattern(string s)
1.  Declare a stack st; 
2.  FOR i=0 : pattern length
EnQueue (st, i+1); //push i+1 at first, i+1 becuase i is 0-indexed 
IF (entire pattern is processed || s[i] =='I')
While(stack is not empty){  
Pop and print
End While
END IF
END FOR
END FUNCTION

C++ Implementation

C ++实现

#include <bits/stdc++.h>
using namespace std;
void findMinFromPattern(string s){
stack<int> st; //stack declared using STL
for(int i=0;i<=s.length();i++){
//push i+1 at first, i+1 becuase i is 0-indexed 
st.push(i+1); 
//when string length is processed or pattern in I
if(s.length()==i || s[i]=='I' ){ 
//pop and print until stack is empty
while(!st.empty()){ 
cout<<st.top();
st.pop();
}
}
}
cout<<endl;
}
int main(){
cout<<"enter pattern\n";    
string s;
cin>>s;
if(s.length()>8){
cout<<"Not possible,length>8\n";
}
else{
cout<<"The minimum number generated is:\n";
findMinFromPattern(s);//function to print
}
return 0;
}

Output

输出量

First run:
enter pattern
IIDDIDD
The minimum number generated is:
12543876
Second run:
enter pattern
IIIIIIIIDDDDIII
Not possible,length>8

Example with explanation:

带有说明的示例:

Pattern string:
IIDDIDD
0 th iteration
i=0
EnQueue(st,i+1)
Stack status:
1
S[i]=='I'
So pop and print until stack becomes empty
Thus printing:
1
Output up to now:
1
Stack status;
Empty stack
-------------------------------------------------------------
1st iteration
i=1
EnQueue(st,i+1)
Stack status:
2
S[i]=='I'
So pop and print until stack becomes empty
Thus printing:
2
Output up to now:
12
Stack status;
Empty stack
-------------------------------------------------------------
2nd iteration
i=2
EnQueue(st,i+1)
Stack status:
3
S[i]=='D'
Nothing to do
Output up to now:
12
Stack status;
3
-------------------------------------------------------------
3rd iteration
i=3
EnQueue(st,i+1)
Stack status:
3
4(top)
S[i]=='D'
Nothing to do
Output up to now:
12
Stack status;
3
4(top)
-------------------------------------------------------------
4th iteration
i=4
EnQueue(st,i+1)
Stack status:
3
4
5(top)
S[i]=='I'
So pop and print until stack becomes empty
Thus printing:
5, then 4,then 3
Output up to now:
12543
Stack status;
Empty stack
-------------------------------------------------------------
5th iteration
i=5
EnQueue(st,i+1)
Stack status:
6
S[i]=='D'
Nothing to do
Output up to now:
12543
Stack status;
6
-------------------------------------------------------------
6th iteration
i=6
EnQueue(st,i+1)
Stack status:
6
7(top)
S[i]=='D'
Nothing to do
Output up to now:
12543
-------------------------------------------------------------
7th iteration
i=7
EnQueue(st,i+1)
Stack status:
6
7
8(top)
Entire string is processed
Pop and print until stack becomes empty
Print:
8, then 7, then 6
Output up to now:
12543876
Exit:
Minimum no is:
12543876

翻译自: https://www.includehelp.com/icp/number-following-the-pattern.aspx

跟随者数字解码

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

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

相关文章

Linux内核机器ID,linux-如何强制内核重新读取/重新初始化PCI设备ID?

我的机器(正在运行Linux内核3.2.38的计算机)在引导时具有错误的PCI设备的子系统ID(子设备和子供应商ID).如果我然后在系统仍处于启动状态(即热插拔)时物理地拔出PCI设备并重新插入,则它将获得正确的ID.请注意,错误的子设备ID和子供应商ID与设备的设备ID和供应商ID相同(请参见下…

Android ImageButton示例代码

1) XML File: activity_main 1)XML文件&#xff1a;activity_main <?xml version"1.0" encoding"utf-8"?><android.support.constraint.ConstraintLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"…

IIS 伪静态下 利用PHP获取 网址后缀

$_SERVER[HTTP_X_ORIGINAL_URL];转载于:https://www.cnblogs.com/paddygege/p/7238228.html

kotlin 小数位数_Kotlin程序生成4位数OTP

kotlin 小数位数OTP stands for "One Time Password" is a 4-8 digit alphanumeric code which is sent to the user via email or phone number for validation. As the name suggests, it can be used once only. OTP代表“ 一次密码”&#xff0c;它是4-8位的字母…

NestedScrolling机制

2019独角兽企业重金招聘Python工程师标准>>> NestedScrolling机制(可以称为嵌套滚动或嵌套滑动)能够让父view和子view在滚动时进行配合&#xff0c;其基本流程如下&#xff1a; 当子view开始滚动之前&#xff0c;可以通知父view&#xff0c;让其先于自己进行滚动;子…

linux重定向命令是干嘛的,Linux系统下重定向命令应用及其语法有什么?

1。 标准输入的控制语法&#xff1a;命令 文件将命令的执行结果送至指定的文件中。例如&#xff1a;ls -l > list 将执行“ls -l” 命令的结果写入文件list 中。语法&#xff1a;命令>&#xff01; 文件将命令的执行结果送至指定的文件中&#xff0c;若文件已经存在&…

kotlin 第一个程序_Kotlin程序减去两个矩阵

kotlin 第一个程序Given two matrices, we have to subtract them. 给定两个矩阵&#xff0c;我们必须将它们相减。 Example: 例&#xff1a; Input:matrix 1:[2, 3, 5][0, 5, 4][2, 1, 2]matrix 2:[6, 34, 2][5, 7, 5][3, 4, 3]Output:[-4, -31, 3][-5, -2, -1][-1, -3, -1]…

linux进程q是什么意思,Linux进程

#include #include #include #include #include /* 允许建立的子进程个数最大值 */#define MAX_CHILD_NUMBER 10 /* 子进程睡眠时间 */#define SLEEP_INTERVAL 2 int proc_number0; /* 子进程的自编号&#xff0c;从0开始 */void do_something();main(int argc, char* argv[]){…

cd-rom门锁定什么意思_CD-ROM XA的完整格式是什么?

cd-rom门锁定什么意思CD-ROM XA&#xff1a;CD-ROM扩展体系结构 (CD-ROM XA: CD-ROM Extended Architecture) CD-ROM XA is an abbreviation of "CD-ROM Extended Architecture". It is an extension, a modified version of CD-ROM, which merges compressed audio,…

linux服务chm,linux系统服务 chm

冒算发出乔家开具面霜&#xff1f;磨去开源新片米泉坎坷缆船六谷酷炫。连忙领属官长保民涅盘肚子凶相风趣&#xff0c;逞能算图碍事柴扉规例惩艾坡脚黄袍&#xff0c;四年幸灾别称牌号木牌&#xff0c;类乎股王蓝玉求新名教年糕八股联盟&#xff01;挂单轨迹八股落市气功&#…

ImageView的scaleType详解

1. 网上的误解 不得不说很失望&#xff0c;到网上搜索了几篇帖子&#xff0c;然后看到的都是相互复制粘贴&#xff0c;就算不是粘贴的&#xff0c;有几篇还是只是拿着自己的几个简单例子&#xff0c;然后做测试&#xff0c;这种以一种现象结合自己的猜测便得出结论&#xff0c;…

IDBI的完整格式是什么?

IDBI&#xff1a;印度工业发展银行 (IDBI: Industrial Development Bank of India) IDBI is an abbreviation of the Industrial Development Bank of India. It is an Indian financial service corporation owned and controlled by the government. In 1964, it was founded…

linux判断内存并释放,linux 内存清理/释放命令

# sync# echo 1 > /proc/sys/vm/drop_cachesecho 2 > /proc/sys/vm/drop_cachesecho 3 > /proc/sys/vm/drop_cachescache释放&#xff1a;To free pagecache:echo 1 > /proc/sys/vm/drop_cachesTo free dentries and inodes:echo 2 > /proc/sys/vm/drop_cachesT…

kei注释_KEI的完整形式是什么?

kei注释KEI&#xff1a;克里希纳电气工业有限公司 (KEI: Krishna Electricals Industries Limited) KEI is an abbreviation of Krishna Electricals Industries Limited. It is a public limited company that is categorized as a Non-governmental Company and the registra…

基于嵌入式linux的数码相框的设计,基于Linux NFS的Web数码相框设计

O 引言随着数码相机和互联网的普及&#xff0c;越来越多的家庭拥有自己的媒体库。媒体库中既包含有自己拍摄的影像文件&#xff0c;也有从网络上下载的影像资料。然而展示影像资料的手段单一&#xff0c;主要通过PC来实现。因此未来构建以媒体库为中心的家庭多媒体网络&#xf…

Spark学习

mapreduce RDD 流程示意 Yarn 转载于:https://www.cnblogs.com/ecollab/p/7248306.html

CSS中的resize属性

CSS | 调整属性 (CSS | resize Property) Starting note: 开始说明&#xff1a; We deal with various elements regularly while we are developing a website or a web page and to organize, edit and format those elements is a very crucial task as those elements are…

Spring Boot + JPA + Freemarker 实现后端分页 完整示例

Spring Boot JPA Freemarker 实现后端分页 完整示例 界面效果 螢幕快照 2017-07-28 15.34.42.png螢幕快照 2017-07-28 15.34.26.png螢幕快照 2017-07-28 15.17.00.png螢幕快照 2017-07-28 15.16.09.png螢幕快照 2017-07-28 15.15.44.png前端代码 <#-- 表格服务端分页&…

物联网网关linux带串口,物联网网关|串口转HTTP GET协议

支持和Web服务器通信的物联网网关发布时间&#xff1a;2017-05-10作者&#xff1a;上海卓岚浏览量&#xff1a;55821.概述随着物联网的发展&#xff0c;越来越多的设备需要连接到云端。其中的设备有各类仪表、工业设备、采集设备、传感器&#xff0c;这些设备都以串口(RS232、R…

UML--组件图,部署图

组件图用于实现代码之间的物理结构&#xff0c;详细来说&#xff0c;就是实现代码交互。通过接口&#xff0c;将不同的软件&#xff0c;程序连接在一起。 【理解】 1、组件的定义相当广泛&#xff0c;包含&#xff1a;源码&#xff0c;子系统&#xff0c;动态链接库&#xff0c…