uva 11988

来源:互联网 发布:淘宝一千零一夜在哪 编辑:程序博客网 时间:2024/05/01 01:42

这其实是一道很简单的模拟题,方法都相近,可能用数学的思维解比较巧妙,不过直白点就用链表模拟,可是由于最近没怎么做题了,一直WA,傻逼的没发现很白的错误,诶,看来做什么都是要持之以恒的,如果只是三天打鱼两天晒网,是根本不可能有什么太大收获,一定要好好做题了。

#include<cstdlib>#include<cstdio>#include<cstring> using namespace std;struct list{     char c;     struct list*nextptr;};struct list *headptr,*leftptr,*rightptr,*currentptr,*temp;int flag;void deal(char ch){     temp=(list*)malloc(sizeof(list));     temp->c=ch;     temp->nextptr=NULL;     if(flag==1){         if(headptr==NULL){              headptr=temp;              currentptr=headptr;         }         else{              currentptr->nextptr=temp;              currentptr=currentptr->nextptr;         }     }     if(flag==2){         if(headptr==NULL){              headptr=temp;              currentptr=temp;              leftptr=temp;              rightptr=NULL;         }         else{              if(leftptr==NULL){                   leftptr=temp;                   leftptr->nextptr=headptr;                   rightptr=headptr;                   headptr=leftptr;              }              else{                   leftptr->nextptr=temp;                   temp->nextptr=rightptr;                   leftptr=temp;              }              if(currentptr->nextptr!=NULL)currentptr=currentptr->nextptr;         }     }}int main(){     char str[100005];     headptr=NULL,leftptr=NULL,rightptr=NULL,currentptr=NULL;     flag=1;     while(scanf("%s",str)!=EOF){            int len=strlen(str);           for(int i=0;i<len;i++){               if(str[i]=='['){                   flag=2;                   leftptr=NULL,rightptr=NULL;               }               else if(str[i]==']')flag=1;               else{                    deal(str[i]);                    }           }           currentptr=headptr;           while(currentptr!=NULL){               printf("%c",currentptr->c);               currentptr=currentptr->nextptr;                     }           headptr=NULL,leftptr=NULL,rightptr=NULL,currentptr=NULL;           flag=1;           printf("\n");     }     return 0;} 


原创粉丝点击