QianYi Ke? 2020-04-04 12:43 采纳率: 100%
浏览 514
已采纳

问题如下,请用C++回答?

请用C++回答
tl;dr: Given a string of digits S, insert a minimum number of opening and closing parentheses into it such that the resulting string is balanced and each digit d is inside exactly d pairs of matching parentheses.
Let the nesting of two parentheses within a string be the substring that occurs strictly between them. An opening parenthesis and a closing parenthesis that is further to its right are said to match if their nesting is empty, or if every parenthesis in their nesting matches with another parenthesis in their nesting. The nesting depth of a position p is the number of pairs of matching parentheses m such that p is included in the nesting of m.
For example, in the following strings, all digits match their nesting depth: 0((2)1), (((3))1(2)), ((((4)))), ((2))((2))(1). The first three strings have minimum length among those that have the same digits in the same order, but the last one does not since ((22)1) also has the digits 221 and is shorter.
Given a string of digits S, find another string S', comprised of parentheses and digits, such that:
all parentheses in S' match some other parenthesis,
removing any and all parentheses from S' results in S,
each digit in S' is equal to its nesting depth, and
S' is of minimum length.
Input
The first line of the input gives the number of test cases, T. T lines follow. Each line represents a test case and contains only the string S.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the string S' defined above.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ length of S ≤ 100.
Test set 1 (Visible Verdict)
Each character in S is either 0 or 1.
Test set 2 (Visible Verdict)
Each character in S is a decimal digit between 0 and 9, inclusive.
Sample

Input

4
0000
101
111000
1

Output

Case #1: 0000
Case #2: (1)0(1)
Case #3: (111)000
Case #4: (1)

The strings ()0000(), (1)0(((()))1) and (1)(11)000 are not valid solutions to Sample Cases #1, #2 and #3, respectively, only because they are not of minimum length. In addition, 1)( and )(1 are not valid solutions to Sample Case #4 because they contain unmatched parentheses and the nesting depth is 0 at the position where there is a 1.
You can create sample inputs that are valid only for Test Set 2 by removing the parentheses from the example strings mentioned in the problem statement.

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-04-04 22:53
    关注
    #include <iostream>
    using namespace std;
    
    char * sol(char * s)
    {
        char * r = new char[1000];
        int l = 0;
        int ri = 0;
        for (int i = 0; s[i]; i++)
        {
            int upl = (s[i] - '0') - l;
            if (upl > 0)
            {
                for (int j = upl; j != 0; j--)
                    r[ri++] = '(';
            }
            else if (upl < 0)
            {
                for (int j = upl; j != 0; j++)
                    r[ri++] = ')';
            }
    
            l=l+upl;
            r[ri++]=s[i];
        }
        for (int i = 0; i < l; i++)
        {
            r[ri++] = ')';
        }
        r[ri] = '\0';
        return r;
    }
    
    int main()
    {
        int n;
        cin >> n;
        char ** result = new char *[n];
        for (int i = 0; i < n; i++)
        {
            char ch[100];
            cin >> ch;
            result[i] = sol(ch);
        }
        for (int i = 0; i < n; i++)
        {
            cout << "Case " << i + 1 << ":" << result[i] << endl;
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多