Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.6 kB
2
Indexable
#include <iostream>
using namespace std;

const int MAX_N = 20001;
const int MAX_M = 1001;
const int MAX_ID = 50001;

int n, m, map[MAX_N][MAX_M], empty[MAX_N], id[MAX_ID];
int curRow, cursor[MAX_N], rowStore[MAX_ID], colStore[MAX_ID];
bool checkEmpty;

void init(int N, int M) {
    n = N;
    m = M;
    
    for(int i = 0; i < MAX_ID; i++) {
        id[i] = 0;
        rowStore[i] = -1;
        colStore[i] = -1;
    }
    
    for(int i = 0; i < n; i++) {
        empty[i] = m;
        cursor[i] = 0;
        for (int j = 0; j < m; j++) {
            map[i][j] = 0;
        }
    }
}

int writeWord(int mId, int mLen) {
    checkEmpty = false;
    
    for(int i = 0; i < n; i++) {
        if(empty[i] >= mLen) {
            curRow = i;
            rowStore[mId] = curRow;
            checkEmpty = true;
            break;
        }
    }
    
    if(!checkEmpty) return -1;

    colStore[mId] = cursor[curRow];
    for(int i = 0; i < mLen; i++) {
        map[curRow][cursor[curRow] + i] = mId;
    }
    
    id[mId] = 1;
    cursor[curRow] += mLen;
    empty[curRow] -= mLen;
    
    return curRow;
}

int eraseWord(int mId) {
    if(id[mId] == 0 || rowStore[mId] == -1 || colStore[mId] == -1) {
        return -1;
    }
    
    int row = rowStore[mId];
    int startCol = colStore[mId];
    int len = 0;
    
    while(startCol + len < m && map[row][startCol + len] == mId) {
        map[row][startCol + len] = 0;
        len++;
    }

    empty[row] += len;
    id[mId] = 0;
    rowStore[mId] = -1;
    colStore[mId] = -1;
    
    return row;
}
Leave a Comment