Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.0 kB
1
Indexable
Never
#include <queue>
#include <string>
#include <string.h>
#include <unordered_map>
using namespace std;
 
#define MAX_N 50001
#define MAX_M 50000
#define MAX_LEN 11
 
int N, M;
 
class word {
public:
    char data[MAX_LEN];
    int length;
};
word myWord[MAX_M];
int used_myWord[MAX_M];
int used_cntWord;
 
class player {
public:
    int id;
    player* next;
    player* prev;
};
player myPlayer[MAX_N];
 
class compare {
public:
    bool operator () (int val1, int val2) {
        if (strcmp(myWord[val1].data, myWord[val2].data) > 0) {
            return true;
        } else {
            return false;
        }
    }
};
priority_queue <int, vector<int>, compare> heapWord[26];
 
unordered_map <string, bool> hashWord;
 
string convertToString (char* val) {
    string res(val);
    return res;
}
 
void reverseString (word* val) {
    char temp;
    for (int i = 0; i < val->length / 2; i++) {
        temp = val->data[i];
        val->data[i] = val->data[val->length - 1 - i];
        val->data[val->length - 1 - i] = temp;
    }
}
 
void init(int N, int M, char mWords[][MAX_LEN])
{
    ::N = N;
    ::M = M;
    hashWord.clear();
    myPlayer[1].id = 1;
    myPlayer[1].prev = &myPlayer[1];
    myPlayer[1].next = &myPlayer[1];
    for (int i = 2; i <= N; i++) {
        myPlayer[i].id = i;
        myPlayer[i].prev = &myPlayer[i - 1];
        myPlayer[i].next = &myPlayer[1];
        myPlayer[1].prev = &myPlayer[i];
        myPlayer[i - 1].next = &myPlayer[i];
    }
    for (int i = 0; i < 26; i++) {
        while (!heapWord[i].empty()) {
            heapWord[i].pop();
        }
    }
    for (int i = 0; i < M; i++) {
        strcpy(myWord[i].data, mWords[i]);
        myWord[i].length = strlen(mWords[i]);
        heapWord[mWords[i][0] - 'a'].push(i);
        hashWord[myWord[i].data] = true;
    }
}
 
int playRound(int mID, char mCh)
{
    used_cntWord = 0;
    int loopID = mID;
    char loopCH = mCh;
 
    while (true) {
        if (heapWord[loopCH - 'a'].empty()) {
            for (int i = 0 ; i < used_cntWord; i++) {
                heapWord[myWord[used_myWord[i]].data[0] - 'a'].push(used_myWord[i]);
            }
            myPlayer[loopID].prev->next = myPlayer[loopID].next;
            myPlayer[loopID].next->prev = myPlayer[loopID].prev;
            return loopID;
        }
 
        int idWord = heapWord[loopCH - 'a'].top();
        heapWord[loopCH - 'a'].pop();
        hashWord[myWord[idWord].data] = true;
        loopID = myPlayer[loopID].next->id;
        loopCH = myWord[idWord].data[myWord[idWord].length - 1];
        hashWord[myWord[idWord].data] = true;
 
        reverseString(&myWord[idWord]);
        string temp = convertToString(myWord[idWord].data);
         
        if (hashWord[temp] == false) {
            used_myWord[used_cntWord] = idWord;
            used_cntWord++;
        }
    }
}