• 上上下下的字符串(模拟)


    上上下下的字符串

    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
  • 相关阅读:
    pip国内源
    高级信息系统项目管理师十大管理优秀范文
    DOM是什么
    js中!!的妙用
    Angular中@Output()的使用方法
    Js数组内对象去重
    JS去除对象或数组中的空值('',null,undefined,[],{})
    JS数组与字符串相互转换
    Js删除数组中的空值
    promise.all的应用场景举例
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9767032.html
Copyright © 2020-2023  润新知