HDU - 6799Parentheses Matching(贪心+栈)

Parentheses Matching

Given a string P consisting of only parentheses and asterisk characters (i.e. “(”, “)” and “"), you are asked to replace all the asterisk characters in order to get a balanced parenthesis string with the shortest possible length, where you can replace each "” by one “(”, or one “)”, or an empty string “”.

A parenthesis string S is a string consisting of only parentheses (i.e. “(” and “)”), and is considered balanced if and only if:

● S is an empty string, or
● there exist two balanced parenthesis strings A and B such that S=AB, or
● there exists a balanced parenthesis string C such that S=©.

For instance, “”, “()”, “(())”, “()()”, “()(())” are balanced parenthesis strings.

Due to some notorious technical inability, if there are several solutions with the shortest possible length, then you have to report the smallest possible one in lexicographical order.

For every two different strings A and B of the same length n, we say A is smaller than B in lexicographical order if and only if there exists some integer k such that:

● 1≤k≤n, and
● the first (k−1) characters of A and that of B are exactly the same, and
● the k-th character of A is smaller than that of B.

For instance, “()(())” is smaller than “()()()”, and in this case, k=4.
Input
There are several test cases.

The first line contains an integer T (1≤T≤105), denoting the number of test cases. Then follow all the test cases.

For each test case, the only line contains a string of length n (1≤n≤105), denoting the string P that consists of only parentheses and asterisk characters.

It is guaranteed that the sum of n in all test cases is no larger than 5×106.
Output
For each test case, output in one line “No solution!” (without quotes) if no solution exists, or otherwise the smallest possible solution in lexicographical order. Note that the output characters are case-sensitive.
在这里插入图片描述
思路: 使左括号尽可能在左边出现,右括号尽可能在右边出现,使用栈利用贪心思想可做。

#include<list>
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<iomanip>
#include<cstdlib>
#include<stdexcept>
#include<fstream>
#include<iterator>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+10;
const int inf = 0x3f3f3f3f;
const int Base =131;
const ll INF = 1ll << 62;
//const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9+9;
#define mem(a,b) memset(a,b,sizeof(a))
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }

int main(){
    int T;
    scanf("%d",&T);
    vector<int>v;
    while(T--){
        int f=1;
        stack<int>st;
        v.clear();
        char s[maxn];
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;i<len;i++){
            if(s[i]=='*')v.push_back(i);
            else if(s[i]=='(')st.push(i);
            else{
                if(!st.empty())st.pop();
                else if(!v.empty()) {
                   s[v.front()]='(';
                   v.erase(v.begin());
                }else{
                    f=0;
                    break;
                }
            }
        }
        while(!st.empty()){
            if(!v.empty()&&v.back()>st.top()){//*的下标应大于(的下标,即)在(的右侧
                s[v.back()]=')';
                st.pop();
                v.pop_back();
            }else{
                f=0;
                break;
            }
        }
        if(f==0)printf("No solution!\n");
        else{
            for(int i=0;i<len;i++){
                if(s[i]!='*')printf("%c",s[i]);
            }
            printf("\n");
        }
    }
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值