acm-week3-链表与队列

时间很紧,简单写一下吧

1、寄包柜

#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+5;

vector < vector<long long> > a;//动态数组

int n,q;

int main()

{

   cin>>n>>q;

   a.resize(n);

   int i,j,k;

   for (int m=1;m<=q;m++)

   {

      int x;

       cin>>x;

       if (x==1)

       {

            //int i,j,k;

            cin>>i>>j>>k;

            if (a[i].size()<j+1)  a[i].resize(j+1);//重载

            a[i][j]=k;//放入

         }

         else if (x==2)

         {

              //int i,j;

              cin>>i>>j;

              cout<<a[i][j]<<endl;

         }

   }

   return 0;

}

2、括号序列

#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 10;

int vis[maxn];

int id[maxn];

char st[maxn];

int top;

int main() {

       string str, ans = "";

       cin >> str;

       for(int i = 0; i < str.size(); i++) {

              if(str[i] == '(' || str[i] == '[') {

                     st[++top] = str[i];//左括号入栈

                     id[top] = i;//标记位置

              }

              else if(str[i] == ')') {

                     if(top && st[top] == '(') {

                            vis[i] = true;//标记

                            vis[id[top]] = true;//标记

                            top--;//弹栈

                     }

              }//配对

              else if(str[i] == ']') {

                     if(top && st[top] == '[') {

                            vis[i] = true;

                            vis[id[top]] = true;

                            top--;

                     }

              }

       }//同理

       for(int i = 0; i < str.size(); i++) {

              if(vis[i] == true)     cout << str[i];

              else {

                     if(str[i] == '(' || str[i] == ')')   cout << "()";

                     else cout << "[]";

              }

       }

       return 0;

}

3、后缀表达式

#include<bits/stdc++.h>

using namespace std;

int sta[105];

int main(){

    int top=0;

    char c;

    int k;

    while (cin>>c && c!='@')

       {

        int num=0;

        if (c>='0'&&c<='9'){

            num=c-'0';//读入,找出第一个数字

            while(cin>>c && c>='0' && c<='9'){

                num=num*10+c-'0';//接着读要是还是数字就增长这个数字

            }

            sta[++top]=num;//入栈

        }

        if (c=='+'){

            sta[top-1]=sta[top]+sta[top-1];

            top--;

        }

        else if (c=='-'){

            sta[top-1]=sta[top-1]-sta[top];

            top--;

        }

        else if (c=='*'){

            sta[top-1]=sta[top-1]*sta[top];

            top--;

        }

        else if (c=='/'){

            sta[top-1]=sta[top-1]/sta[top];

            top--;

        }

    }//运算

    cout<<sta[1];

    return 0;

}

4、队列安排//主要按资料里的板子来

#include<bits/stdc++.h>

using namespace std;

const int N=1e5+5;

int n,m;

struct T

{

       int l,r;

       int d;

}t[N]={0};//结构体定义

void add(int i,int k,int p)   

{

    if(p==1)      

    {

        t[k].r=t[i].r;

        t[k].l=i;

        t[i].r=k;

        t[t[k].r].l=k;

    }

    else            

    {

        t[k].r=i;

        t[k].l=t[i].l;

        t[i].l=k;

        t[t[k].l].r=k;

    }

}//加入元素

void del(int x)

{

    if (t[x].l == 0 && t[x].r == 0) return;

    t[t[x].l].r = t[x].r;

    t[t[x].r].l = t[x].l;

    t[x].l = t[x].r = 0;

}//删除元素

int main()

{

   int x,k,p;

   cin>>n;

   t[0].r=0,t[0].l=0;

   add(0,1,1);

   for (int i=2;i<=n;i++)

    {

        cin>>x>>p;

        add(x,i,p);//加入

    }

    cin>>m;

    for (int i=1;i<=m;i++)

       {

             cin>>x;

             del(x);//删掉

       }

      

    for (int i=t[0].r;i;i=t[i].r)//遍历(需要记住)

    {

              cout<<i<<" ";

       }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值