SDUT 栈和队列专题

来源:互联网 发布:js plumb 编辑:程序博客网 时间:2024/05/20 22:38
#include<bits/stdc++.h>using namespace std;typedef int ElemType;#define OK 1#define FAIL -1#define MAXSIZE 100010#define SIZEINCREMENT 100010typedef struct node{    ElemType *base;    int length;} Stack;Stack *init(){    Stack *s;    s = new node;    s->base = new ElemType[MAXSIZE];    if(!s->base)        exit(FAIL);    s->length = 0;    return s;}int Empty(Stack *s){    if(s->length == 0)        return 1;    else        return 0;}int Push(Stack *s, ElemType x){    if(s->length >= MAXSIZE)        return 0;    s->length++;    s->base[s->length] = x;    return 1;}int Pop(Stack *s){    if(!Empty(s))    {        s->length--;        return 1;    }    else        return 0;}int Top(Stack *s){    if(!Empty(s))        return s->base[s->length];    else        return -1;///返回负值}int convert(char a){    if(a == '*' || a == '/')        return 2;    else if(a == '+' || a == '-')        return 1;    else        return 0;}void solve()///一般算术表达式转换成后缀式{    Stack *s;    s = init();    char suf[200], in[200];    int i, j;    scanf("%s", in);    for(i = 0, j = 0; in[i] != '#'; i++)    {        if(in[i] >= 'a' && in[i] <= 'z')            suf[j++] = in[i];        else if(Empty(s) || Top(s) == '(' || in[i] == '(')            Push(s, in[i]);        else if(in[i] == ')')        {            while(!Empty(s) && Top(s) != '(')            {                suf[j++] = Top(s);                Pop(s);            }            if(!Empty(s))                Pop(s);        }        else        {            while((!Empty(s)) && (convert(Top(s)) >= convert(in[i])))            {                suf[j++] = Top(s);                Pop(s);            }            Push(s, in[i]);        }    }    while(!Empty(s))    {        suf[j++] = Top(s);        Pop(s);    }    suf[j] = 0;    puts(suf);}void solve1()///后缀式求值{    char a[1000];    Stack *s;    s = init();    scanf("%s", a);    for(int i = 0; a[i] != '#'; i++)    {        if(a[i] >= '0' && a[i] <= '9')            Push(s, a[i] - '0');        else        {            int x, y;            if(!Empty(s)) y = Top(s);            Pop(s);            if(!Empty(s)) x = Top(s);            Pop(s);            switch(a[i])            {            case '+':                x += y;                Push(s, x);                break;            case '-':                x -= y;                Push(s, x);                break;            case '*':                x *= y;                Push(s, x);                break;            case '/':                x /= y;                Push(s, x);                break;            }        }    }    cout << Top(s) << endl;}void solve2()///括号匹配(use gets()){    char a[100];    Stack *s;    int flag;    while(gets(a))    {        flag = 1;        s = init();        int len = strlen(a);        for(int i = 0; i < len; i++)        {            if(a[i] == '(' || a[i] == '{' || a[i] == '[')                Push(s, a[i]);            else            {                if(a[i] == ')')                {                    if(Top(s) == '(')                        Pop(s);                    else                    {                        flag = 0;                        break;                    }                }                else if(a[i] == '}')                {                    if(Top(s) == '{')                        Pop(s);                    else                    {                        flag = 0;                        break;                    }                }                else if(a[i] == ']')                {                    if(Top(s) == '[')                        Pop(s);                    else                    {                        flag = 0;                        break;                    }                }            }        }        if(Empty(s) && flag)            cout << "yes" << endl;        else            cout << "no" << endl;    }}void solve3()///栈的基本操作{    int t, m, n, k;    char x;    Stack *s;    cin >> t;    while(t--)    {        s = init();        cin >> m >> n;        getchar();        for(int i = 0; i < n; i++)        {            cin >> x;            switch(x)            {                case 'P':{                    cin >> k;                    if(s->length >= m) cout << 'F' << endl;                        else                        {                            getchar();                            Push(s, k);                        }                        break;}                case 'O':                    if(Empty(s)) cout << 'E' << endl;                    else                    {                        cout << Top(s) << endl;                        Pop(s);                    }                    break;                case 'A':                    if(Empty(s)) cout << 'E' << endl;                    else                    {                        cout << Top(s) << endl;                    }                    break;            }        }        cout << endl;    }}void solve4()///行编辑器{    Stack *s;    char a[1000];    int i = 0;    while(gets(a))    {        s = init();        for(i = 0; a[i] != 0; i++)        {            if(a[i] == '#')            {                Pop(s);            }            else if(a[i] == '@')            {                s->length = 0;            }            else                Push(s, a[i]);        }       i = 1;       while(s->length >= i)        printf("%c", s->base[i++]);       printf("\n");    }}void solve5()///出栈序列判定(Don't use char as the ElemType){    Stack *s;    int a[10100], b[10100], i, j, n, t;    scanf("%d", &n);    for(i = 0; i < n; i++)        cin >> a[i];    cin >> t;    while(t--)    {        for(i = 0; i < n; i++)            cin >> b[i];        s = init();        j = 0;        for(i = 0; i < n; i++)        {            Push(s, a[i]);            while(!Empty(s) && Top(s) == b[j])            {                Pop(s);                j++;            }        }        if(j == n)            cout << "yes" << endl;        else            cout << "no" << endl;    }}///进制转换void solve6(Stack *s)///这里注意要判断零的情况{    int n, m;    cin >> n >> m;    if(n == 0)        printf("0\n");    else    {        while(n)        {            Push(s, n % m);            n /= m;        }        while(!Empty(s))        {            printf("%d", Top(s));            if(!Empty(s))                Pop(s);        }        printf("\n");    }}int main(){    solve5();    return 0;}

原创粉丝点击