有人帮吗,mx求救
查看原帖
有人帮吗,mx求救
966277
lovecjiajia楼主2023/9/27 13:28
#include <stdio.h>
#include <stdbool.h>

#define MAX_SIZE 500001

int read() {
    char c = getchar();
    int x = 0, f = 1;
    while (c < '0' || c > '9') {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

bool iscut[MAX_SIZE]; // 标记是否为切割点

// 哈希表结构
typedef struct {
    int value;
    bool is_exist;
} HashTable;

int main() {
    int n, m, i;
    n = read();
    m = read();

    int a[MAX_SIZE];
    for (i = 1; i <= n; i++) {
        a[i] = read();
    }
    
    int b[MAX_SIZE];
    for (i = 1; i <= m; i++) {
        b[i] = read();
    }

    HashTable hash_table[MAX_SIZE] = {0}; // 初始化哈希表
    for (i = 1; i <= m; i++) {
        hash_table[b[i]].is_exist = true;
    }

    int cnt = 0;
    iscut[0] = true;
    for (i = 1; i <= n; i++) {
        if (hash_table[a[i]].is_exist) {
            iscut[i] = true;
        }
        if (iscut[i-1] && !iscut[i]) {
            cnt++;
        }
    }
    printf("%d", cnt);
    return 0;
}
2023/9/27 13:28
加载中...