当前位置:编程学习 > C/C++ >>

poj 1064 Parencodings(模拟题)

到这个时间,虽然不是很晚,但是挺累的,白天是在实训,晚上要去准备考研!回来的时候把这个题目给写了,现在得赶紧去睡觉了,明天还要早起打卡,唉!

算法的具体思想明天在实验室的时候找个时间来写!


[cpp]
#include <iostream>  
#include <stack>  
using namespace std; 
#define MAX 100  //整个的括号的长度不会超过50   
 
/*284K  0MS*/  
int main() 

    int t; 
    cin>>t; 
    while(t--) 
    { 
        int n; 
        cin>>n; 
        int *a=new int[n]; 
        for(int i=0;i<n;i++) 
           cin>>a[i]; 
        char str[MAX]; 
        int count=0; 
        int index=0; 
        for(int i=0;i<n;i++) 
        { 
            int j; 
            for(j=count;j<a[i];j++) 
                str[index++]='('; 
            str[index++]=')'; 
            count=a[i]; 
        } 
        str[index]='\0'; 
        int *b=new int[n]; 
        int p=0; 
        //堆栈操作  
        stack<char> store; 
        for(int i=0;i<index;i++) 
        { 
            if(str[i]=='(') 
              store.push('('); 
            else 
            {    
                int num=0; 
                while(store.top()!='(') 
                { 
                    num++; 
                    store.pop(); 
                }     
                num++; 
                b[p++]=num; 
                store.pop(); 
                for(int k=0;k<num;k++) 
                  store.push('*'); 
            }  
        } 
      
        for(int i=0;i<p-1;i++) 
           cout<<b[i]<<" "; 
        cout<<b[p-1]<<endl; 
        delete []b; 
        delete []a; 
    } 
     
    system("pause"); 
    return  0; 

#include <iostream>
#include <stack>
using namespace std;
#define MAX 100  //整个的括号的长度不会超过50

/*284K 0MS*/
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int *a=new int[n];
        for(int i=0;i<n;i++)
           cin>>a[i];
        char str[MAX];
        int count=0;
        int index=0;
        for(int i=0;i<n;i++)
        {
            int j;
            for(j=count;j<a[i];j++)
                str[index++]='(';
            str[index++]=')';
            count=a[i];
        }
        str[index]='\0';
        int *b=new int[n];
        int p=0;
        //堆栈操作
        stack<char> store;
        for(int i=0;i<index;i++)
        {
            if(str[i]=='(')
              store.push('(');
            else
            {  
                int num=0;
                while(store.top()!='(')
                {
                    num++;
                    store.pop();
   &n

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,