数据结构实验

(一)线性表
1

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    multiset<int> ms;
    cin >> n;
    for(int i = 1;i <= n;i ++) {
        int x;
        cin >> x;
        ms.insert(x);
    }
    int x;
    cin >> x;
    ms.insert(x);
    for(int i:ms) 
        cout << i << ",";
    return 0;
}

2

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    map<int , int> mp;
    set<int> s;
    cin >> n;
    for(int i = 1;i <= n;i ++) {
        int x , y;
        cin >> x >> y;
        s.insert(y);
        mp[y] += x;
    }

    cin >> n;
    for(int i = 1;i <= n;i ++) {
        int x , y;
        cin >> x >> y;
        s.insert(y);
        mp[y] += x;
    }

    if(s.size() == 0) {
        cout << "0 0";
        return 0;
    }

    bool flag = 0;
    for(auto it = s.end();;) {
        it --;
        int i = *it;
        if(mp[i] != 0) {
            flag = 1;
            cout << mp[i] << ' ' << i << ' ';
        }
        if(it == s.begin()) break;
    }
    if(flag == 0) cout << 0 << ' ' << 0;
    return 0;
}

3

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int>a(n + 1);
    for(int i = 1;i <= n;i ++) 
        cin >> a[i];
    for(int i = n;i >= 1;i --) 
        cout << a[i] << ' ';
    return 0;
}

4

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 100;
int pre[N] , lst[N] , a[N];

void del(int x) {
    int p = pre[x] , l = lst[x];
    pre[l] = p;
    lst[p] = l;
}

void solve() {
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++) cin >> a[i];
    for(int i = 2;i < n;i ++) {
        pre[i] = i - 1;
        lst[i] = i + 1;
    }
    pre[1] = n;
    lst[1] = 2;
    lst[n] = 1;
    pre[n] = n - 1;
    int now = 1 , syg;
    for(int i = 1;i <= 5;i ++) 
        now = lst[now];
    cout << now << " ";
    syg = a[now];
    del(now);
    n --;
    while(n --) {
        while(syg --) now = lst[now];
        syg = a[now];
        del(now);
        cout << now << ' ';
    }
    cout << "\n";
}

int main() {
    int t;
    cin >> t;
    while(t --) 
        solve();
    return 0;
}

(二)堆栈
1

#include <bits/stdc++.h>
#define int long long
using namespace std;

bool solve() {
    string s;
    cin >> s;
    int len = s.size();
    s = " " + s;
    stack<char> st;
    for(int i = 1;i <= len;i ++) {
        if(s[i] == '(' || s[i] == '[' || s[i] == '{') {
            st.push(s[i]);
        } else {
            if(s[i] == ')') {
                if(st.top() == '(') st.pop();
                    else return false;
            }
            if(s[i] == ']') {
                if(st.top() == '[') st.pop();
                    else return false;
            }
            if(s[i] == '}') {
                if(st.top() == '{') st.pop();
                    else return false;
            }
        }
    }
    if(st.size() == 0) return true;
    return false;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0) , cout.tie(0);
    cout << (solve()?"True":"False");
    return 0;
}

2

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 50;
char ch[N] = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F'};

void work(int x,int c) {
    if(x == 0) return ;
    work(x / c , c);
    cout << ch[x % c];
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0) , cout.tie(0);
    int n , m;
    cin >> n >> m;
    if(n == 0) cout << 0;else
    work(n , m);
    return 0;
}

3

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) ((int)((x).size()))
#define lb(x) ((x) & (-(x)))
#define bp(x) __builtin_popcount(x)
#define bpll(x) __builtin_popcountll(x)
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;

ll ksm(ll x, ll y) {
    ll res = 1;
    while (y) {
        if (y & 1) {
            res = res * x;
        }
        x = x * x;
        y >>= 1;
    }
    return res;
}

void solve() {
    string s;
    cin >> s;

    int cur = 0;
    vector<pii> a;
    for (int i = 0; i < SZ(s); i++) {
        char c = s[i];
        if (isdigit(c)) {
            cur = cur * 10 + (c - '0');
            if (i == SZ(s) - 1 || !isdigit(s[i + 1])) {
                a.push_back({cur, 0});
                cur = 0;
            }
        } else {
            a.push_back({c, 1});
        }
    }

    stack<int> num;
    stack<char> opt;
    map<char, int> mp;
    mp['+'] = mp['-'] = 1;
    mp['*'] = mp['/'] = 2;
    mp['^'] = 3;
    mp['('] = 0;

    auto calc = [&](int x, int y, char c) -> int {
        if (c == '+') {
            return x + y;
        } else if (c == '-') {
            return x - y;
        } else if (c == '*') {
            return x * y;
        } else if (c == '/') {
            return x / y;
        } else if (c == '^') {
            return ksm(x, y);
        } else {
            assert(false);
        }
        return 114514;
    };

    int n = SZ(a);
    for (int i = 0; i < n; i++) {
        if (a[i].se == 0) {
            num.push(a[i].fi);
        } else {
            char c = a[i].fi;
            if (c == '(') {
                opt.push(c);
            } else if (c == ')') {
                while (true) {
                    char op = opt.top();
                    opt.pop();
                    if (op == '(') {
                        break;
                    }
                    int y = num.top(); num.pop();
                    int x = num.top(); num.pop();
                    int res = calc(x, y, op);
                    num.push(res);
                }
            } else {
                if (opt.empty()) {
                    opt.push(c);
                } else if (mp[c] > mp[opt.top()]) {
                    opt.push(c);
                } else {
                    while (!opt.empty() && mp[opt.top()] >= mp[c]) {
                        int op = opt.top();
                        opt.pop();
                        int y = num.top(); num.pop();
                        int x = num.top(); num.pop();
                        int res = calc(x, y, op);
                        num.push(res);
                    }
                    opt.push(c);
                }
            }
        }
    }

    while (!opt.empty()) {
        int op = opt.top();
        opt.pop();
        int y = num.top(); num.pop();
        int x = num.top(); num.pop();
        int res = calc(x, y, op);
        num.push(res);
    }
    assert(SZ(num) == 1);
    cout << num.top() << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    while (T--) solve();
    return 0;
}

(三)队列

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    queue<int> q1 , q2;
    int n; cin >> n;
    vector<int> a(n + 1);
    for(int i = 1;i <= n;i ++) {
        cin >> a[i];
        if(a[i] & 1) q1.push(a[i]);
            else q2.push(a[i]);
    }
    while(q1.size() > 1 && q2.size() > 0) {
        cout << q1.front() << ' ';
        q1.pop();
        cout << q1.front() << ' ';
        q1.pop();
        cout << q2.front() << ' ';
        q2.pop();
    }
    while(q1.size()) {
        cout << q1.front() << ' ';
        q1.pop();
    }
    while(q2.size()) {
        cout << q2.front() << ' ';
        q2.pop();
    }
    return 0;
}

2

#include <stdio.h>
#include <stdlib.h>
#define ERROR -1
typedef int ElementType;
typedef int Pos;
typedef struct SNode *Stack;
struct SNode
{
    ElementType *Data;
    Pos Top;
    int MaxSize;
};
Stack Creastack(int size)
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Data = (ElementType *)malloc(size * sizeof(ElementType));
    S->Top = -1;
    S->MaxSize = size;
    return S;
}
int IsFull(Stack S)
{
    return S->Top == (S->MaxSize - 1);
}
int IsEmpty(Stack S)
{
    return S->Top == -1;
}
void Push(Stack S, ElementType item)
{
    if (IsFull(S))
    {
        printf("ERROR:Full\n");
    }
    else
        S->Data[++S->Top] = item;
}
ElementType Pop(Stack S)
{
    if (IsEmpty(S))
    {
        return ERROR;
    }
    else
    {
        return S->Data[S->Top--];
    }
}
void AddQ(ElementType item, Stack S1, Stack S2)
{
    if (IsFull(S1) && !IsEmpty(S2))
    {
        printf("ERROR:Full\n");
        return;
    }
    if (IsFull(S1) && IsEmpty(S2))
    {
        while (!IsEmpty(S1))
        {
            Push(S2, Pop(S1));
        }
    }
    Push(S1, item);
}
ElementType DeleteQ(Stack S1, Stack S2)
{
    if (IsEmpty(S1) && IsEmpty(S2))
    {
        printf("ERROR:Empty\n");
        return ERROR;
    }
    if (IsEmpty(S2))
    {
        while (!IsEmpty(S1))
        {
            Push(S2, Pop(S1));
        }
    }
    return Pop(S2);
}
int main()
{
    int N1, N2, item;
    char chao;
    Stack S1, S2;
    scanf("%d %d", &N1, &N2);
    S1 = Creastack(N2);
    S2 = Creastack(N1);
    while (scanf(" %c", &chao) != EOF && chao != 'T')
    {
        if (chao == 'A')
        {
            scanf(" %d", &item);
            AddQ(item, S1, S2);
        }
        else if (chao == 'D')
        {
            ElementType ele = DeleteQ(S1, S2);
            if (ele != ERROR)
                printf("%d\n", ele);
        }
    }
    return 0;
}

(四)树与二叉树
1

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 100;
int cnt = 1 , seat = 1;
char node[N];
int num;
int lson[N] , rson[N];
string s;

void Build(int &k) {
    if(s[seat] == '#') return ;
    if(k == 0) k = ++cnt;
    node[k] = s[seat];
    seat ++; Build(lson[k]);
    seat ++; Build(rson[k]);
}

void pre(int k) {
    cout << node[k];
    if(lson[k] != 0) pre(lson[k]);
    if(rson[k] != 0) pre(rson[k]);
}

void mid(int k) {
    if(lson[k] != 0) mid(lson[k]);
    cout << node[k];
    if(rson[k] != 0) mid(rson[k]);
}

void lst(int k) {
    if(lson[k] == 0 && rson[k] == 0) num ++;
    if(lson[k] != 0) lst(lson[k]);
    if(rson[k] != 0) lst(rson[k]);
    cout << node[k];
}

bool jing() {
    int len = s.size() - 1;
    for(int i = 1;i <= len;i ++) 
        if(s[i] != '#') return true;
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> s;
    int n = s.size();
    s = " " + s;

    if(jing() == 0) {
        cout << 0;
        return 0;
    }
    int root = 1;
    Build(root);
    pre(1);
    cout << "\n";
    mid(1);
    cout << "\n";
    lst(1);
    cout << "\n";
    cout << num;
    return 0;
}

2

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 100;
int cnt = 1 , seat = 1;
char node[N];
int num;
int lson[N] , rson[N] , dep[N];
vector<char> G[N];
string s;

void Build(int fa , int &k) {
    if(s[seat] == '#') return ;
    if(k == 0) k = ++cnt;
    dep[k] = dep[fa] + 1;
    G[dep[k]].push_back(s[seat]);
    node[k] = s[seat];
    seat ++; Build(k , lson[k]);
    seat ++; Build(k , rson[k]);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> s;
    int n = s.size();
    s = " " + s;

    int root = 1;
    Build(1 , root);

    for(int i = 1;i <= n;i ++) 
        for(char now:G[i]) cout << now;
    return 0;
}

3

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 100;
vector<int> G[N];
char shit[N];
int zhi[N];
int fa[N] , cnt;
string s;
priority_queue<pair<int , int> , vector<pair<int , int> > , greater<pair<int , int> > > pq;

void dfs(int x) {
    if(x == cnt) return ;
    dfs(fa[x]);
    cout << zhi[x];
}

signed main() {
    int i = 0;
    while(cin >> s) {
        i ++;
        shit[i] = s[0];
        int cd = s.size();
        string now = s.substr(2 , cd - 2);
        stringstream ss;
        ss << now;
        int qz;
        ss >> qz;
        pq.push(make_pair(qz , i));
    }
    cnt = i;
    while(pq.size() > 1) {
        int s1 = pq.top().first , n1 = pq.top().second;
        pq.pop();
        int s2 = pq.top().first , n2 = pq.top().second;
        pq.pop();

        cnt ++;
        fa[n1] = cnt , fa[n2] = cnt;
        zhi[n1] = 0 , zhi[n2] = 1;
        pq.push({s1 + s2 , cnt});
    }

    for(int k = 1;k <= i;k ++) {
        cout << shit[k] << ":";
        dfs(k);
        cout << "\n";
    }

    return 0;
}

(五)图
1

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 100;
vector<int> G[N];
int fa[N];
int cnt = 0;
bool vis[N];

void dfs(int s) {
    vis[s] = 1;
    cnt ++;
    cout << s << ' ';
    for(int i = G[s].size() - 1;i >= 0;i --) {
        int v = G[s][i];
        if(vis[v]) continue;
        dfs(v);
    }
}

signed main() {
    int n , m , s;
    cin >> n >> m >> s;
    for(int i = 1;i <= m;i ++) {
        int x , y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(s);
    if(cnt != n) cout << "\n" << 0;
    return 0;
}

2

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 100;
vector<int> G[N];
int fa[N];
int cnt = 0;
bool vis[N];

void bfs(int s) {
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    while(q.size()) {
        cnt ++;
        int now = q.front();
        cout << now << ' ';
        q.pop();
        for(int i = G[now].size() - 1;i >= 0;i --) {
            int v = G[now][i];
            if(vis[v] == 1) continue;
            vis[v] = 1;
            q.push(v);
        }
    }
}

signed main() {
    int n , m , s;
    cin >> n >> m >> s;
    for(int i = 1;i <= m;i ++) {
        int x , y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    bfs(s);
    if(cnt != n) cout << "\n" << 0;
    return 0;
}

3

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 100;
int cnt;
struct edge{
    int x , y , val;
}e[N];
int fa[N];

int findpa(int x) {
    if(x == fa[x]) return x;
    return fa[x] = findpa(fa[x]);
}

bool compare(edge s1,edge s2) {
    return s1.val < s2.val;
}

signed main() {
    int x , y , z;
    while(cin >> x >> y >> z) {
        if(x == 0 && y == 0 && z == 0) break;
        e[++cnt].x = x;
        e[cnt].y = y;
        e[cnt].val = z;
    }
    sort(e + 1 , e + 1 + cnt , compare);
    for(int i = 1;i <= 1e6;i ++) fa[i] = i;
    int ans = 0;
    for(int i = 1;i <= cnt;i ++) {
        int x = e[i].x , y = e[i].y;
        int fx = findpa(x) , fy = findpa(y);
        if(fx == fy) continue;
        fa[fx] = fy;
        ans += e[i].val;
    }
    cout << ans;
    return 0;
}

(六)查找
1

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e5 + 100;
int n , head , tail;
vector<int> G[N];
int a[N] , xl[N] , cnt = 0;

void solve(int n) {
    vector<int> a(n + 1);
    for(int i = 1;i <= n;i ++) 
        cin >> a[i];
    sort(a.begin() + 1 , a.begin() + 1 + n);
    for(int i = 1;i <= n;i ++) 
        cout << a[i] << ' ';
    cout << "\n";
    int m;
    cin >> m;
    for(int i = 1;i <= m;i ++) {
        int x;
        cin >> x;
        int now = lower_bound(a.begin() + 1 , a.begin() + 1 + n , x) - a.begin();
        if(now == 0 || now == n + 1 || a[now] != x) {
            cout << 0 << ' ';
        } else cout << now << ' ';
    }
    cout << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0) , cout.tie(0);
    int t;
    while(cin >> t) solve(t);
    return 0;
}

2

#include <iostream>
#include <cstdio>
#define int long long
using namespace std;

const int N = 1e6 + 100;
int a[N] , Left[N]  , Right[N] , tree[N] , k = 1;

int add(int x,int v) {
    if(x == 0) {
        x = ++k;
        tree[x] = v;
        return x;
    }
    if(v <= a[x]) Left[x] = add(Left[x] , v);
        else Right[x] = add(Right[x] , v);
    return x;
}

void qx(int x) {
    if(x == 0) return ;
    cout << tree[x] << ' ';
    qx(Left[x]);
    qx(Right[x]);
}

signed main(){
    int cnt = 0;
    while(cin >> a[++cnt]) {
        if(a[cnt] == 0) break;
    }
    cnt --;
    tree[1] = a[1];
    for(int i = 2;i <= cnt;i ++) {
        add(1 , a[i]);
    }
    qx(1);
    return 0;
}

3

#include<iostream>
#include<cstdlib>
#include<queue>
using namespace std;
typedef struct BinaryTree_Node{
    int key;
    BinaryTree_Node* lChild;
    BinaryTree_Node* rChild;
}*BT_Node;
void DeleteNode(BT_Node &node){
    BT_Node temp=node;
    if(node->rChild==NULL&&node->lChild==NULL){
        node=node->lChild;
        free(temp);
    }else if(node->lChild!=NULL){
        BT_Node left=node->lChild;
        while (left->rChild!=NULL){
            temp=left;
            left=left->rChild;
        }
        node->key=left->key;
        if(temp!=node)DeleteNode(temp->rChild);
        else{
            DeleteNode(temp->lChild);
        }
    }else if(node->lChild==NULL&&node->rChild!=NULL){
        BT_Node right=node->rChild;
        while (right->lChild!=NULL){
            temp=right;
            right=right->lChild;
        }
        node->key=right->key;
        if(temp!=node)DeleteNode(temp->lChild);
        else{
            DeleteNode(temp->rChild);
        }
    }
}
void printf(BT_Node node){
    bool flag=false;
    queue<BT_Node> q;
    q.push(node);
    while (!q.empty()){
        BT_Node temp=q.front();
        if(flag)cout<<" ";
        flag=true;
        cout<<temp->key;
        q.pop();
        if(temp->lChild!=NULL)q.push(temp->lChild);
        if(temp->rChild!=NULL)q.push(temp->rChild);
    }
    cout<<endl;
}
void Delete(BT_Node &node,int key){
    if(node==NULL){
        return;
    }
    if(key==node->key){
        DeleteNode(node);
        return;
    }
    if(key<node->key){
        Delete(node->lChild,key);
    }else{
        Delete(node->rChild,key);
    }
}

void insert(BT_Node &node,int key){
    if(node==NULL){
        node=(BT_Node)malloc(sizeof(BinaryTree_Node));
        node->lChild=NULL;
        node->rChild=NULL;
        node->key=key;
        return;
    }
    if(key<node->key){
        insert(node->lChild,key);
    }else{
        insert(node->rChild,key);
    }
}

int main(){
	BT_Node root=NULL;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int key;
        cin>>key;
        insert(root,key);
    }
    int m;
    cin>>m;
    for (int i = 0; i < m; ++i) {
        int temp;
        cin>>temp;
        Delete(root,temp);
    }
    printf(root);
}

4

#include<stdio.h>
#include<stdlib.h>
int arr1[18];
int arr2[18];
int main() {
	int n, num, i, temp;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &num);
		arr2[i] = num;
		temp = num % 17;
		while (arr1[temp] != 0) {
			temp = (temp + 5) % 18;
		}
		arr1[temp] = num;
	}
	for (i = 0; i < n; i++) {
		for (int j = 0; j < 18; j++) {
			if (arr1[j] != 0 && arr1[j] == arr2[i]) {
				printf("%d pos: %d\n", arr1[j], j);
			}
		}
	}
	return 0;
}
posted @   NEUQ-zyb  阅读(11)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示