1. 程式人生 > >上上下下的字符串(模擬)

上上下下的字符串(模擬)

algorithm names -- gif color 字符 必須 out 要求

上上下下的字符串

https://code.mi.com/problem/list/view?id=60&cid=0&sid=55230#codearea

描述

字符串 S 由字符 +- 構成。字符串 D 是一個數字字符串,其長度比 S 要大 1,其格式要求如下:

  1. D 中不包含數字 0;
  2. D 中必須包含數字 1,且最大數字不大於 D 的長度;
  3. D 中的數字不重復出現。

根據 S,可以轉換得到唯一的 D,S 與 D 的關系為:

  1. S[i] 為 + 表示 D[i] < D[i+1];
  2. S[i] 為 - 表示 D[i] > D[i+1],且 D[i] - D[i+1] = 1.

現給出字符串 S 的值,請構造出合法的字符串 D 。 如輸入 +-+-,輸出 13254,因為 1 < 3 > 2 < 5 > 4,符合增減增減(+-+-)的趨勢。

輸入

只由 + 和 - 構成的一個字符串。

輸出

一個不含0且沒有重復數字的字符序列。

輸入樣例

++++
----
+-+-++

復制樣例

輸出樣例

12345
54321
1325467


找出‘+‘的位置,‘-‘的位置的值與‘+‘的位置的值對比下就行了
技術分享圖片
 1 #include<iostream>
 2 #include<cmath>
 3
#include<vector> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 #include<cstdio> 8 #include<map> 9 #include<queue> 10 #define maxn 200005 11 #define mem(a,b) memset(a,b,sizeof(a)) 12 typedef long long ll; 13 using namespace std; 14 int a[10005
]; 15 16 int main(){ 17 string s; 18 while(cin>>s){ 19 s="0"+s; 20 s+="+"; 21 int co=1; 22 int minn=0x3f3f3f3f; 23 a[0]=1; 24 int pre=0; 25 int num=1; 26 for(int i=1;i<s.length();i++){ 27 if(s[i]==+){ 28 a[pre]=num++; 29 pre=i; 30 } 31 else{ 32 num++; 33 } 34 } 35 cout<<a[0]; 36 num=a[0]; 37 for(int i=1;i<pre;i++){ 38 if(s[i]==-){ 39 cout<<--num; 40 } 41 else{ 42 cout<<a[i]; 43 num=a[i]; 44 } 45 } 46 cout<<endl; 47 } 48 }
View Code

上上下下的字符串(模擬)