吉林大学2023数据结构《第四次上机实验》解题报告

本文介绍了四道编程实验题目,涉及序列调度、稀疏矩阵操作、前缀查询、方案计数以及凇鼠与雪花问题。这些题目展示了栈、哈希、拓扑排序等数据结构在实际问题中的应用,同时讨论了时间复杂度优化和算法策略。
摘要由CSDN通过智能技术生成

《第四次上机实验》解题报告

1  序列调度

思想;非常基础的一道题,完全就是模拟栈的工作,关键就是在栈的长度过大的时候break。

时间复杂度;O(n)

总结;在查找的时候如果没找到就应该反复压栈,直至找到,中间如果超长度就输出no。

#include<iostream>
#include<queue>
#include <utility>
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
int x[1001000] = {};
int y[1001000];
int main()
{
    int t, c, a, b,d,e,p1=1,p2,f=1;
    scanf("%d %d", &t, &c);
    for (a = 1; a <= t; a++)
    {
        scanf("%d", &b);
        for (d = 1; d <= b; d++)
            scanf("%d", &y[d]);
        p2 = 1;
        p1 = 1;
        e = 1;
        f = 1;
        for (d = 1; d <= b; d++)
        {
            while (x[p1 - 1] != y[p2])
            {
                x[p1] = e;
                e++;
                p1++;
                if (p1 > c + 1)
                {
                    f = 0;
                    break;
                }
            }
            p2++;
            if (p1 > c + 1)
            {
                f = 0;
                break;
            }
            p1--;
        }
        if (f == 0)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}

2  稀疏矩阵之差

思想;我用的map模拟的稀疏矩阵,定义了map<int,int>x[51000];这样map就变成了一个三维数组,之后每次输入就直接在里面存了。后两个矩阵相减,要注意可能减为0,那时候不能输出,同时还要用count判断对应的值是否存在,要不然map会报错

时间复杂度;O(t)

总结;map相比数组虽然可以省去很多不必要的内存,但操作比较复杂,尤其在加减时要判断对应的值是否存在,不能直接加或者减。

#include<iostream>
#include<queue>
#include <utility>
#include<stdio.h>
#include<vector>
#include<cstring>
#include<map>
using namespace std;
map<int,int>x[51000];
int main()
{
    int n1, m1, t1,n2,m2,t2 ,a, b, r, c, v,t=0;
    scanf("%d %d %d", &n1, &m1, &t1);
    for (a = 1; a <= t1; a++)
    {
        scanf("%d %d %d", &r, &c, &v);
        x[r][c] = v;
        if(v!=0)
        t++;
    }
    scanf("%d %d %d", &n2, &m2, &t2);
    for (a = 1; a <= t2; a++)
    {
        scanf("%d %d %d", &r, &c, &v);
        if (x[r].count(c) == 0)
        {
            x[r][c] = -1 * v;
            t++;
        }
        else
        {
            if (x[r][c] == 0)
                t++;
            x[r][c] -= v;
            if (x[r][c] == 0)
                t--;
        }
    }
    if (n1 == n2 && m1 == m2)
    {
        printf("%d %d %d\n", n1, m1, t);
        for (a = 1; a <= n1; a++)
        {
            if (x[a].size() > 0)
            {
                for (auto it = x[a].begin(); it != x[a].end(); it++) {
                    if(it->second!=0)
                    printf("%d %d %d\n", a, it->first, it->second);
                }
            }
        }
    }
    else
        printf("Illegal!");
    return 0;
}

3  前缀查询

思想;将所有输入的字符串的所有前缀全部变为对应的哈希值,然后将哈希值存入map当中,后面查找时,将要查找的字符串按相同的对应关系也变为哈希值,在map中能直接查找。

时间复杂度;O(n*log n)

总结;在考试的时候我的哈希对应方程一直有哈希冲突,试了5,6次一直解决不了,考完后,我随便改了一个数字就tmd过了,太气了。

#include<iostream>
#include<queue>
#include <utility>
#include<stdio.h>
#include<vector>
#include<cstring>
#include<string.h>
#include<map>
using namespace std;
map<long long, int>x;
char y[21];
int main()
{
    int a, b, c, d, n, m;
    long long t, s;
    scanf("%d %d", &n, &m);
    for (a = 1; a <= n; a++)
    {
        scanf("%s", y);
        getchar();
        t = 0;
        s = 0;
        for (b = 0; b <= strlen(y); b++)
        {
            t = (b + 1711) * (b + 114514) * y[b] + 883128 * b + 66232366 + 2113 * y[b] + 73333 * y[b] * b + t * y[b] * b;
            s += t;
            if (x.count(s) == 0)
                x[s] = 1;
            else
                x[s]++;
        }
    }
    for (a = 1; a <= m; a++)
    {
        scanf("%s", y);
        getchar();
        s = 0;
        t = 0;
        for (b = 0; b < strlen(y); b++)
        {
            t = (b + 1711) * (b + 114514) * y[b] + 883128 * b + 66232366 + 2113 * y[b] + 73333 * y[b] * b+ t * y[b] * b;
            s += t;
        }
        if (x.count(s) == 0)
            printf("0\n");
        else
            printf("%d\n", x[s]);
    }
    return 0;
}

4  方案计数

思想;用的拓扑排序+添加一个数组记录所要的最长时间+高精度。最后遍历所有的点,把最长时间对应的方案数用高精度相加。

时间复杂度;O(n^2)

总结第2行,1个整数,关键方案数,最多100位。这句话我在考试的时候看成了最多一百条方案,结果就是没用高精度,一直在那里调,调了半个多小时

#include<stdio.h>
#include<vector>
using namespace std;
vector<int>ve[10010];
vector<int>re[10010];
int path[10010][110];
int tpath[110];
int zhan[1001000];
int ru[10010] = {};
int quan[10010];
int time[10010];
int main()
{
    int n, m, a, b, c, d, p=1, q=1,t1,t2,t3;
    scanf("%d %d", &n, &m);
    for (a = 1; a <= n; a++)
    {
        scanf("%d", &quan[a]);
    }
    for (a = 1; a <= m; a++)
    {
        scanf("%d %d", &b, &c);
        ve[b].push_back(c);
        re[c].push_back(b);
        ru[c]++;
    }
    for (a = 1; a <= n; a++)
    {
        if (ru[a] == 0)
        {
            zhan[q] = a;
            time[a] = quan[a];
            path[a][0] = 1;
            path[a][1] = 1;
            q++;
        }
    }
    for (p = 1; p <= q; p++)
    {
        t1 = zhan[p];
        for (a = 0; a < ve[t1].size(); a++)
        {
            t2 = ve[t1][a];
            ru[t2]--;
            if (ru[t2] == 0)
            {
                t3 = 0;
                zhan[q] = t2;
                q++;
                for (b = 0; b < re[t2].size(); b++)
                {
                    if (time[re[t2][b]] > t3)
                    {
                        t3 = time[re[t2][b]];
                        for (c = 0; c <= 101; c++)
                        {
                            path[t2][c] = path[re[t2][b]][c];
                        }
                    }
                    else if (time[re[t2][b]] == t3)
                    {
                        for (c = 1; c <= 101; c++)
                        {
                            path[t2][c] += path[re[t2][b]][c];
                        }
                        for (c = 1; c <= 101; c++)
                        {
                            path[t2][c + 1] += path[t2][c] / 10;
                            path[t2][c] %= 10;
                        }
                    }
                }
                time[t2] = quan[t2] + t3;
            }
        }
    }
    t1 = 1;
    for (a = 1; a <= n; a++)
    {
        if (path[a][0] != 1)
            t1 = 0;
    }
    if (t1 == 0)
    {
        printf("0");
    }
    else
    {
        t1 = 0;
        for (a = 1; a <= n; a++)
        {
            if (time[a] > t1)
            {
                t1 = time[a];
                for (b = 1; b <= 101; b++)
                    tpath[b] = path[a][b];
            }
            else if (time[a] == t1)
            {
                for (b = 1; b <= 101; b++)
                {
                    tpath[b] += path[a][b];
                }
                for (b = 1; b <= 101; b++)
                {
                    tpath[b + 1] += tpath[b] / 10;
                    tpath[b] %= 10;
                    if (tpath[b] != 0)
                        c = b;
                }
            }
        }
        printf("%d\n", t1);
        for (b = c; b >=1; b--)
            printf("%d", tpath[b]);
    }
    return 0;
}

 

5  凇鼠与雪花

思想

时间复杂度

总结;  

#include<stdio.h>
#include <iostream>
using namespace std;
int n, m;
struct cell {
    int ls, rs, dist, val;
}tr[100100];
int fa[100100];
bool del[100100];
int find(int a)
{
    if (fa[a]==a)
        return a;
    return fa[a] = find(fa[a]);
}
int merge(int x, int y) {    //合并
    int t;
    if (!x || !y) return x | y;   //???不理解?????
    if (tr[x].val < tr[y].val || (tr[x].val == tr[y].val && x > y))
    swap(x,y);
    tr[x].rs = merge(tr[x].rs, y);
    if (tr[tr[x].ls].dist < tr[tr[x].rs].dist)  
     swap(tr[x].ls,tr[x].rs);
    tr[x].dist = tr[tr[x].rs].dist + 1;
    return x;
}
void erase(int x)
{
    del[x] = 1;
    fa[x] = fa[tr[x].ls] = fa[tr[x].rs] = merge(tr[x].ls, tr[x].rs);
}
int main()
{
    int a, b, c,d,e,f;
    scanf("%d %d", &n, &m);
    for (a = 1; a <= n; a++)
    {
        scanf("%d", &b);
        fa[a] = a;
        tr[a].val = b;
    }
    for (a = 1; a <= m; a++)
    {
        scanf("%d", &b);
        if (b == 1)
        {
            scanf("%d %d", &c, &d);
            if (find(c)==find(d)||del[c]||del[d])
                continue;
            c = find(c);
            d = find(d);
            fa[c] =fa[d]= merge(c,d);
        }
        if (b == 2)
        {
            scanf("%d", &c);
            if (del[c])
                continue;
            erase(find(c));
        }
        if (b == 3)
        {
            scanf("%d", &c);
            if (del[c]) {
                printf("-1\n");
                continue;
            }
            printf("%d\n", tr[find(c)].val);
        }
    }
    return 0;
}
 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值