• 51Nod2594 括号之价


    Problem

    小Y上数据结构课的时候摸鱼,听到老师在讲用栈做括号匹配,于是乎边随意写了一个合法的括号序列。但是光是写括号太无聊了,他现在想知道这个括号序列的价值。他是这样定义一个括号序列的价值的:

    1、一对括号价值一分(比如"()"得一分)

    2、两个合法的括号序列的拼接而成的括号序列的价值是他们的价值的和(比如"()()"价值为1+1=2)

    3、嵌套的括号的序列的价值是,所嵌套的括号序列的价值的翻倍(比如"((()))"价值为122=4)

    下课了,qz看到小Y写的括号序列,他一眼就推测出了规则并得到了括号序列的价值。那么问题来了,小Y写下的括号序列的价值是多少呢?

    Solution

    赋值深度,然后找最近的深度dfs。

    题解是把价值也放进去,每次碰到右括号就全加起来*2。

    Code

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<stack>
    #define mod 998244353LL
    #define mem(ss) memset(ss,0,sizeof(ss))
    #define ll long long
    #define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    using namespace std;
    ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
    inline int read(){int data=0;char ch=0;while (ch<'0' || ch>'9') ch=getchar();while (ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();return data;}
    ll lowspeed(ll a,ll b,ll p){ll cur=a,ans=0;while(b){if(b&1) ans=(ans+cur)%p;cur=(cur+cur)%p;b>>=1;}return ans%p;}
    ll speed(ll a,ll b,ll p){ll cur=a,ans=1;while(b){if(b&1) ans=lowspeed(ans,cur,p)%p;cur=lowspeed(cur,cur,p)%p;b>>=1;}return ans%p;}
    string s;
    int a[120];
    int dfs(int l,int r,int val){
        if(r==l+1){
            return 1;
        }
        int sum=0,a1=0,a2=0;
        for(int i=l+1;i<r;i++){
            if(a[i]==val+1){
                if(a1==0){
                    a1=i;
                }
                else{
                    a2=i;
                    sum+=dfs(a1,a2,val+1);
                    a1=a2=0;
                }
            }
        }
        return sum*2;
    }
    int main(){
        io_opt;
        cin>>s;
        int dep=0;
        for(int i=0;i<s.size();i++){
            if(s[i]=='('){
                dep++;
                a[i]=dep;
            }
            else{
                a[i]=dep;
                dep--;
            }
        }
    
        int ans=0,x1=-1,x2=-1;
        for(int i=0;i<=s.size();i++){
            if(a[i]==1){
                if(x1==-1){
                    x1=i;
                }
                else{
                    x2=i;
                    ans+=dfs(x1,x2,1);
    
                    x1=x2=-1;
                }
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    selenium介绍
    python爬虫之requests模块介绍
    SQLAlchemy框架用法详解
    JS判断是否为移动版浏览器
    goahead Web Server 环境搭建(Linux)
    Android 应用层APP发送短信
    Git使用相关问题汇总
    Spring boot 默认首页配置
    Android Studio高版本中文输入异常
    Android ADB 常用命令详解
  • 原文地址:https://www.cnblogs.com/sz-wcc/p/11621947.html
Copyright © 2020-2023  润新知