[Data Structure] 小测回顾(4378 和4379)

这是数据结构课的第一次当堂小测

 

小测形式为教室集中,线上。

题目

T1 4378 金融操控家

https://acm.sjtu.edu.cn/OnlineJudge/problem/4378

一道特别水的题。只要记住当前的最小,每次输入求差值的最大值,并更新最小值即可

#include<bits/stdc++.h>

using namespace std;

const int MAXN=1e6+5;

int n,ans;
int now,minn;

void read(int &p)
{
    char s=getchar();
    p=0;
    while(!isdigit(s)) s=getchar();
    for(;isdigit(s);s=getchar()) p=p*10+s-'0';
}

int main()
{
    read(n);
    read(now);minn=now;
    for(int i=1;i<n;i++)
    {
        read(now);
        minn=min(minn,now);
        ans=max(now-minn,ans);
    }
    cout<<ans;
    return 0;
}

 

T2 4379 字符串匹配

https://acm.sjtu.edu.cn/OnlineJudge/problem/4379

常规的3个括号匹配,只需要建个栈,每次输入左括号就入栈,右括号则查看是否有对应的左括号在栈顶,无则出错,最后栈为空也错

(注意,这里的输入没有“”)

#include<bits/stdc++.h>

using namespace std;

char s[11005];
int st[11005],now;

int ask(char s)
{
    switch(s)
    {
        case '(':return 1;
        case ')':return -1;
        case '{':return 2;
        case '}':return -2;
        case '[':return 3;
        case ']':return -3;
    }
    return 0;
}

int main()
{
    //cin>>s;
    cin.getline(s,10005);
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int tem=ask(s[i]);
        if(tem==0) continue;
        
        if(tem>0) st[++now]=tem;
        if(tem<0)
        {
            if((!now)||(now&&tem+st[now]!=0))
            {
                cout<<0;
                return 0;
            }
            else now--;
        }
    }
    if(!now) cout<<1;
    else cout<<0;
    return 0;
}

 

posted @ 2021-04-11 15:32  Adaxy  阅读(225)  评论(0编辑  收藏  举报