括號(hào)匹配(二)nyoj

#include<stdio.h>  
#include<string.h>  
  
char _stack[205];  
int head = -1;  
int rear;  
void pop()  
{  
    head--;  
}  
  
void push(char ch)  
{  
    _stack[++head] = ch;  
}  
  
char top()  
{  
    return _stack[head];  
}  
  
int main()  
{  
    int n;  
    int i;  
    int j;  
    int cnt;  
    int run;  
    char str[105];  
    scanf("%d",&n);  
    while(n--)  
    {  
        cnt = 0;  
        head = -1;  
        scanf("%s",str);  
        for(i=0; i<strlen(str); i++)  
        {  
            switch(str[i])  
            {  
            case '(':  
            case '[':  
                push(str[i]);  
                break;  
            case ']':  
                if(top()=='[')  
                    pop();  
                else if(top()=='(')  
                {  
                    run = 1;  
                    for(j=0; j<head; j++)  
                    {  
                        if(_stack[j]=='[')  
                        {  
                            pop();  
                            cnt++;  
                            i--;  
                            run = 0;  
                            break;  
                        }  
                    }  
                    if(run)  
                        cnt++;  
                }  
                else  
                    cnt++;  
                break;  
            case ')':  
                if(top()=='(')  
                    pop();  
                else if(top()=='[')  
                {  
                    run = 1;  
                    for(j=0; j<head; j++)  
                    {  
                        if(_stack[j]=='(')  
                        {  
                            pop();  
                            cnt++;  
                            i--;  
                            run = 0;  
                            break;  
                        }  
                    }  
                    if(run)  
                        cnt++;  
                }  
                else  
                    cnt++;  
                break;  
            default:  
                break;  
            }  
        }  
        cnt+=head+1;  
        printf("%d\n",cnt);  
    }  
}  

RT記錄一下 標(biāo)程式DP也帖一下以后看

#include<iostream>  
#include<cstdio>  
#include<cstring>  
  
  
using namespace std;  
const int maxn=110;  
char tab[maxn];  
int f[maxn][maxn];  
  
  
int fun(int i,int j)  
{  
    if(i>j)return 0;  
    if(f[i][j]>=0)return f[i][j];  
    if(i==j)return f[i][j]=1;  
    int va=maxn;  
    if((tab[i]=='('&&tab[j]==')') || (tab[i]=='['&&tab[j]==']'))  
        va=fun(i+1,j-1);  
    for(int mid=i;mid<j;mid++){  
        va=min(va,fun(i,mid)+fun(mid+1,j));  
    }  
    return f[i][j]=va;  
}  
  
  
int main()  
{  
       
    int n;  
    scanf("%d",&n);  
    for(int i=0;i<n;i++){  
        memset(f,-1,sizeof(f));  
        memset(tab,0,sizeof(tab));  
        scanf("%s",tab);  
        cout<<fun(0,strlen(tab)-1)<<endl;  
    }  
}          
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容