跟随者数字解码

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
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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

跟随者数字解码

跟随者数字解码_跟随模式的数字相关推荐

  1. matlab朴素贝叶斯手写数字识别_从“手写数字识别”学习分类任务

    机器学习问题可以分为回归问题和分类问题,回归问题已经在线性回归讲过,本文学习分类问题.分类问题跟回归问题有明显的区别,回归问题是连续的数值,而分类问题是离散的类别,比如将性别分为[男,女],将图片分为 ...

  2. 数字练习_为什么要练习数字简约

    数字练习 重点 (Top highlight) Disclaimer: This article does not contain any affliate links Back in 2018, R ...

  3. python数字识别_利用Python进行数字识别

    思路 通过Python实现KNN算法.而KNN算法就是K最近邻(k-Nearest Neighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一.该方法的思路是:如果一 ...

  4. python 仪表数字识别_利用Python进行数字识别

    思路 通过Python实现KNN算法.而KNN算法就是K最近邻(k-Nearest Neighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一.该方法的思路是:如果一 ...

  5. qregexp限制数字范围_计算一列数字的平均值

    考虑一下下面的的问题: 你有一列浮点类型的数字.这绝不是令人讨厌的恶作剧----没有无穷个数字或无限大的数字,仅仅只是正常的"简单的"浮点型的数字. 现在:计算其平均值.你能做到吗 ...

  6. java编写数字金字塔_用JAVA写数字金字塔

    今年的蓝桥杯中我遇到了一道题是关于数字金字塔的,那时候在比赛时可能是各方面的因素有思路但是没有实现,直到今天回过头来看,其实只要思路正确了题目就会迎刃而解了,其实数字金字塔的的解题思路就是把金字塔分成 ...

  7. java的标识符可以以数字开头_标识符可以以数字开头,但不能是Java中的关键字...

    标识关于推进两岸交流合作说法正确的有() 数字店招的图片格式,一般默认为( )格式. 但的关海报设计中常运用三大元素,不包括( ). 键字网店商品主图的放大功能至少需要让图片满足( )像素. 标识关于 ...

  8. dsa数字签名算法_密码学中的数字签名算法(DSA)

    dsa数字签名算法 Digital Signature Algorithm (DSA) is one of the Federal Information Processing Standard fo ...

  9. h5 数字变化_基于JS实现数字动态变化显示效果附源码

    先给大家展示下效果,感觉不错,可以参考实现代码,文末附有源码哦. 1.目标 以液晶电子表样式,动态变化的在指定元素内显示数字. 目标关键词:动态变化(定时器),指定元素(DOM元素ID),数字(num ...

最新文章

  1. 五连阳回调买入法_“4连阳+1阴”这种股票,吃透主升浪!挣得万贯家财
  2. 句号一定要划在句子最美的地方
  3. java中的%%%_JSP页面中%!%与%%与%=%
  4. 简洁易懂:c:out标签详解
  5. elasticsearch完全匹配
  6. 微信读书vscode插件_vscode 常用的插件
  7. ABP框架 v2.7.0已经发布!
  8. Windows 7 SID 修改
  9. ShardingSphere(五) 公共表配置,实现读写改操作
  10. Head First设计模式读书笔记六 第七章下 外观模式
  11. python 爬取视频真实地址_java_爬虫_从腾讯视频播放界面爬取视频真实地址
  12. 【数据挖掘】关联规则和Apriori算法
  13. Dump+mysql导入_mysql mysqldump导入数据库
  14. CSS width中的max-content,min-content,fit-content的区别
  15. 【个人学年总结】大一——“蛰伏”的一年
  16. 德普测试仪EOL软件,电池充放电测试系统 EOL
  17. 使用javamail、阿里云邮箱发送邮件
  18. 关于Java程序员技能和面试注意事项
  19. 设备管理的方法与思路-设备管理软件系统-璞华大数据
  20. Unity3d 所有版本下载

热门文章

  1. 如何避免mysql回表查询_mysql如何避免回表查询
  2. linux内核中cent文件夹,Centos 中如何快速定制二进制的内核 RPM 包
  3. 0 0/2 * * * ? linux文本含义,Linux基础2.0
  4. mysql临时表 清空_在数据库中临时表什么时候会被清除呢
  5. Linux基础命令(1)
  6. linux对硬盘进行分区吗,Linux下如何对硬盘进行分区
  7. 各种损失损失函数的使用场景和使用方法:KL散度
  8. pytorch 训练过程学习率设置衰减
  9. MIP改造常见问题二十问
  10. 们--加强斐波那契【递推】