zoukankan      html  css  js  c++  java
  • C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C

    C. Serval and Parenthesis Sequence
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.

    In his favorite math class, the teacher taught him the following interesting definitions.

    parenthesis sequence is a string, containing only characters "(" and ")".

    correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition.

    We define that |s||s| as the length of string ss. A strict prefix s[1l]s[1…l] (1l<|s|)(1≤l<|s|) of a string s=s1s2s|s|s=s1s2…s|s| is string s1s2sls1s2…sl. Note that the empty string and the whole string are not strict prefixes of any string by the definition.

    Having learned these definitions, he comes up with a new problem. He writes down a string ss containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in ss independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.

    After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.

    Input

    The first line contains a single integer |s||s| (1|s|31051≤|s|≤3⋅105), the length of the string.

    The second line contains a string ss, containing only "(", ")" and "?".

    Output

    A single line contains a string representing the answer.

    If there are many solutions, any of them is acceptable.

    If there is no answer, print a single line containing ":(" (without the quotes).

    Examples
    input
    Copy
    6
    (?????
    
    output
    Copy
    (()())
    input
    Copy
    10
    (???(???(?
    
    output
    Copy
    :(
    
    Note

    It can be proved that there is no solution for the second sample, so print ":(".

    解题思路:

    括号匹配问题,但要求 严格前缀不满足括号匹配,但是整个字符满足括号匹配。

    解题思路:

    括号匹配easy,给一组数据:

    8

    ((??))))

    答案:(((())))

    如何处理 ' ? ' 这里有一个贪心, 就是如果满足括号匹配,那么左括号和右括号肯定各占一半。

    先扫一遍,统计已出现的左括号和右括号,

    然后再扫一遍 如果遇到 ' ? ' 先满足达到 N/2 个 左括号(左括号肯定越前越好), 然后再满足 N/2 个右括号。

    最后扫一遍判断修改后的字符串是否满足括号匹配,如果中途出现栈空的情况说明有严格前缀也符合括号匹配,则不行;最后栈不为空也不行,否者输出答案。

    AC code:

     1 #include <bits/stdc++.h>
     2 #define INF 0x3f3f3f3f
     3 #define LL long long
     4 #define inc(i, j, k) for(int i = j; i <= k; i++)
     5 #define rep(i, j, k) for(int i = j; i < k; i++)
     6 #define mem(i, j) memset(i, j, sizeof(i))
     7 #define gcd(i, j) __gcd(i, j)
     8 using namespace std;
     9 string ss;
    10 stack<char>mmp;
    11 int N, a, b;
    12 int main()
    13 {
    14     scanf("%d", &N);
    15     cin >> ss;
    16     if(N%2){ puts(":("); return 0;}
    17     for(int i = 0; i < N; i++)
    18     {
    19         if(ss[i] == '(') a++;
    20         else if(ss[i] == ')') b++;
    21     }
    22     bool flag = true;
    23     int n1 = N/2-a;
    24     int n2 = N/2-b;
    25     int k = 0;
    26     for(int i = 0; i < n1;k++){
    27         if(ss[k] == '?'){ ss[k] = '('; i++;}
    28     }
    29     for(int j = 0; j < n2; k++){
    30         if(ss[k] == '?'){ ss[k] = ')';j++;}
    31     }
    32 //    cout << ss << endl;
    33     for(int i = 0; i < N; i++){
    34         if(mmp.size() == 0){
    35             mmp.push(ss[i]);
    36             continue;
    37         }
    38         if(ss[i] == '('){
    39             mmp.push(ss[i]);
    40         }
    41         else{
    42             if(mmp.top() == '(') mmp.pop();
    43             else mmp.push(ss[i]);
    44             if(mmp.size() == 0 && i != N-1){ flag = false;break; }
    45         }
    46     }
    47     if(mmp.size() == 0 && flag) cout << ss << endl;
    48     else puts(":(");
    49 
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    App开放接口api安全性—Token签名sign的设计与实现
    查看文件(或文件夹)被哪个进程使用【文件已在另一程序中打开】
    利用递归将数组转码
    h5 定位
    使用OAuth Server PHP实现OAuth2服务
    在Linux上安装jdk,mysql,tomcat的准备工作
    Core Java笔记
    随机森林简介
    Linux 查看操作系统版本
    RNA_seq GATK 最佳实践
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10707579.html
Copyright © 2011-2022 走看看