UVA673-括号平衡

#include <iostream>
#include <string>
using namespace std;
struct point
{
    char ch;
    point *next;
};
int main ()
{
    int n, len, flag = 0, flag2 = 0;
    string s;
    cin>>n;
    cin.get();
    while(n--)
    {
        point *p, *q, *head, *bridge;
        getline(cin,s);
        if(!s.size())
        {
            cout<<"Yes"<<endl;
            continue;
        }
        else if(s.size()%2)
        {
            cout<<"No"<<endl;
            continue;
        }
        head = p = new(point);
        len = s.size();
        int i;
        for(i = 0; i < len-1; i++)
        {
            p->ch = s[i];
            p->next = new(point);
            p = p->next;
        }
        p->ch = s[i];
        for(len = s.size(),flag2 = 0,q = head,flag = 0;q!=p&&len; q = flag?head:q->next,flag2 = flag?flag:flag2,flag = 0)
        {
            if((q->ch == '('&& q->next->ch == ')')||(q->ch == '['&& q->next->ch == ']'))
            {
                flag2 = 1;
                if(q->next == p)
                {
                    p = bridge;
                }
                else
                {
                    if(q == head)head = q->next->next;
                    else bridge->next = q->next->next;
                }
                delete q;
                delete q->next;
                flag = 1;
                len-=2;
            }
            else
                bridge = q;
        }
        cout<<(len?"No":"Yes")<<endl;
    }
    return 0;
}


链表问题,用数组也可以做。我代码用时504,人家的最少用时还是0.00,咳咳,我真的很想看他们的代码!神码啊!

posted on 2012-08-21 10:21  Primo...  阅读(108)  评论(0编辑  收藏  举报