跟隨者數字解碼_跟隨模式的數字

跟隨者數字解碼

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.pswp.cn/news/540883.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540883.shtml
英文地址,請注明出處:http://en.pswp.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…