zoukankan      html  css  js  c++  java
  • sdut oj 2376 Expressions

    http://acm.sdut.edu.cn/web/problem.php?action=showproblem&problemid=2376

    题目的意思就是给你一串字符串,其中,大写字母代表的是运算符,小写字母代表的是操作数,然后自定义一种运算形式为。给的这串字符用栈来进行运算,最后会得到一个新的序列,

    题目让你找的是一种序列,这种序列用同样的运算形式,经过队列的运算后得到的新序列与栈运算得到的序列相同。

    其实思路就是很简单,对给定的序列按二叉树的后序遍历形式建树,然后从左向右,从下到上进行层序遍历便可得到结果。建立二叉树不难,难的是当时做的时候没有想到层序遍历是可以bfs实现的,还是fg提醒说bfs就行了,才知道。下面这个代码是比较复杂,因为没有用stl中的栈,是自己又写的一个

      1 #include<stdio.h>
    2 #include<string.h>
    3 #include<iostream>
    4 #include<stdlib.h>
    5 #include<queue>
    6 using namespace std;
    7 #define N 10001
    8 char str[N],sbr[N];
    9 int cnt;
    10 typedef struct node
    11 {
    12 char data;
    13 node *lch,*rch;
    14 }node ,*bitree;
    15 typedef struct sqstack
    16 {
    17 int size;
    18 bitree *top;
    19 bitree *base;
    20 }sqstack;
    21 sqstack strstack;
    22 void initsqstack(sqstack &S)
    23 {
    24 S.base = (bitree*)malloc(N*sizeof(node));
    25 if(!S.base) exit(-1);
    26 S.top = S.base;
    27 S.size = N;
    28 }
    29 void push(sqstack &S,bitree t)
    30 {
    31 if(S.top-S.base >= S.size) exit(-1);
    32 else
    33 *S.top++ = t;
    34 }
    35 void pop(sqstack &S,bitree &t)
    36 {
    37 if(S.top == S.base) exit(-1);
    38 else t = *--S.top;
    39 }
    40 void gettop(sqstack S,bitree &t)
    41 {
    42 if(S.top == S.base) exit(-1);
    43 else t = *(S.top-1);
    44 }
    45 void creattree(bitree &T,bitree lch,bitree rch)
    46 {
    47 T->lch = lch;
    48 T->rch = rch;
    49 if(T->lch && T->rch)
    50 {
    51 if(T->lch->data <= 'z' && T->lch->data >= 'a')
    52 {
    53 lch->lch = lch->rch = NULL;
    54 }
    55 if(T->rch->data <= 'z' && T->rch->data >= 'a')
    56 {
    57 rch->lch = rch->rch = NULL;
    58 }
    59 }
    60 }
    61 void cexperssion(bitree &T)
    62 {
    63 initsqstack(strstack);
    64 node *p,*g,*ll,*rr;
    65 node *value;
    66 if(!(value = (node *)malloc(sizeof(node)))) exit(-1);
    67 value->data='@';//首先压入栈中一个标志符,好作为处理表达式完的一种判断
    68 push(strstack,value);
    69 int i=0;
    70 while(str[i])
    71 {
    72 if(str[i]>='a'&&str[i]<='z')
    73 {
    74 value=(node *)malloc(sizeof(node));
    75 value->data=str[i];
    76 push(strstack,value);
    77 }
    78 else
    79 {
    80 gettop(strstack,p);
    81 if(p->data!='@')
    82 {
    83 g=(node *)malloc(sizeof(node));
    84 g->data=str[i];
    85 pop(strstack,ll);
    86 pop(strstack,rr);
    87 creattree(g,ll,rr);
    88 push(strstack,g);
    89 }
    90 }
    91 i++;
    92 }
    93 T=g;
    94 }
    95 void bfs(bitree T)
    96 {
    97 queue<bitree>qu;
    98 qu.push(T);
    99 node *p;
    100 while(!qu.empty())
    101 {
    102 p=qu.front();
    103 qu.pop();
    104 sbr[cnt++]=p->data;
    105 if(p->rch) qu.push(p->rch); //注意入队顺序
    106 if(p->lch) qu.push(p->lch);
    107 }
    108 }
    109 void output()
    110 {
    111 int i;
    112 for(i=cnt-1;i>=0;i--)
    113 cout<<sbr[i];
    114 cout<<endl;
    115 }
    116 int main()
    117 {
    118 bitree Tree;
    119 int t;
    120 cin>>t;
    121 while(t--)
    122 {
    123 memset(sbr,0,sizeof(sbr));
    124 cin>>str;
    125 cexperssion(Tree);
    126 cnt=0;
    127 bfs(Tree);
    128 output();
    129 }
    130 return 0;
    131 }

    再附加一个把使用stl中的栈的代码简单的多了

      1 #include<stdio.h>
    2 #include<string.h>
    3 #include<iostream>
    4 #include<stdlib.h>
    5 #include<queue>
    6 #include<stack>
    7 using namespace std;
    8 #define N 10001
    9 char str[N],sbr[N];
    10 int cnt;
    11 typedef struct node
    12 {
    13 char data;
    14 node *lch,*rch;
    15 }node ,*bitree;
    16 void creattree(bitree &T,bitree lch,bitree rch)
    17 {
    18 T->lch = lch;
    19 T->rch = rch;
    20 if(T->lch && T->rch)
    21 {
    22 if(T->lch->data <= 'z' && T->lch->data >= 'a')
    23 {
    24 lch->lch = lch->rch = NULL;
    25 }
    26 if(T->rch->data <= 'z' && T->rch->data >= 'a')
    27 {
    28 rch->lch = rch->rch = NULL;
    29 }
    30 }
    31 }
    32 void cexperssion(bitree &T)
    33 {
    34 stack<bitree>st; // 定义栈,栈中的节点类型为 bitree
    35 node *p,*g,*ll,*rr;
    36 node *value;
    37 if(!(value = (node *)malloc(sizeof(node)))) exit(-1);
    38 value->data='@';
    39 st.push(value);
    40 int i=0;
    41 while(str[i])
    42 {
    43 if(str[i]>='a'&&str[i]<='z')
    44 {
    45 value=(node *)malloc(sizeof(node));
    46 value->data=str[i];
    47 st.push(value);
    48 }
    49 else
    50 {
    51 p=st.top();
    52 if(p->data!='@')
    53 {
    54 g=(node *)malloc(sizeof(node));
    55 g->data=str[i];
    56 ll=st.top();
    57 st.pop();
    58 rr=st.top();
    59 st.pop();
    60 creattree(g,ll,rr);
    61 st.push(g);
    62 }
    63 }
    64 i++;
    65 }
    66 T=g;
    67 }
    68 void bfs(bitree T)
    69 {
    70 queue<bitree>qu;
    71 qu.push(T);
    72 node *p;
    73 while(!qu.empty())
    74 {
    75 p=qu.front();
    76 qu.pop();
    77 sbr[cnt++]=p->data;
    78 if(p->rch) qu.push(p->rch); //注意入队顺序
    79 if(p->lch) qu.push(p->lch);
    80 }
    81 }
    82 void output()
    83 {
    84 int i;
    85 for(i=cnt-1;i>=0;i--)
    86 cout<<sbr[i];
    87 cout<<endl;
    88 }
    89 int main()
    90 {
    91 bitree Tree;
    92 int t;
    93 cin>>t;
    94 while(t--)
    95 {
    96 memset(sbr,0,sizeof(sbr));
    97 cin>>str;
    98 cexperssion(Tree);
    99 cnt=0;
    100 bfs(Tree);
    101 output();
    102 }
    103 return 0;
    104 }
  • 相关阅读:
    linux系统中如何进入退出vim编辑器,方法及区别
    [转]JAVA的动态代理机制及Spring的实现方式
    mybaties 缓存
    全面分析 Spring 的编程式事务管理及声明式事务管理
    面试(4)-spring-Spring面试题和答案
    vector的多套遍历方案
    【QT】QT下载与安装
    【QT】无需写connect代码关联信号和槽函数
    【QT】第一个QT程序(点击按钮,显示特定文本)
    【QT】error: 'SIGNAL' was not declared in this scope
  • 原文地址:https://www.cnblogs.com/fxh19911107/p/2389795.html
Copyright © 2011-2022 走看看