TA_Code_Example
#include <iostream> #include <set> #include <string> #include <string.h> #include <unordered_map> using namespace std; #define MALE "male" #define FEMALE "female" #define MAX_STU 20000 #define MAX_GRADE 3 #define GENDER_CNT 2 struct student{ int id; int score; int gender; // male = 0, female = 1; int grade; // 1->3 bool isAvaiable; }pool[MAX_STU+5]; int poolCnt = 1; unordered_map<int, int> hashID; struct CMP{ bool operator()(int a, int b){ if(pool[a].score == pool[b].score) return pool[a].id < pool[b].id; return pool[a].score < pool[b].score; } }; set<int, CMP> studentSet[MAX_GRADE][GENDER_CNT]; void init() { poolCnt = 1; pool[0].id = -1; pool[0].score = -1; hashID.clear(); for(int i = 0; i < MAX_GRADE; i++){ for(int j = 0; j < GENDER_CNT; j++) studentSet[i][j].clear(); } return; } int add(int mId, int mGrade, char mGender[7], int mScore) { int idx = poolCnt++; pool[idx].score = mScore; pool[idx].grade = mGrade-1; pool[idx].gender = strcmp(mGender, MALE) == 0 ? 0 : 1; pool[idx].id = mId; pool[idx].isAvaiable = true; hashID[mId] = idx; studentSet[pool[idx].grade][pool[idx].gender].insert(idx); auto last = --studentSet[pool[idx].grade][pool[idx].gender].end(); return pool[*last].id; } int remove(int mId) { auto it = hashID.find(mId); if(it == hashID.end()) return 0; int idx = hashID[mId]; if(pool[idx].isAvaiable == false) return 0; pool[idx].isAvaiable = false; studentSet[pool[idx].grade][pool[idx].gender].erase(idx); if(studentSet[pool[idx].grade][pool[idx].gender].size() == 0) return 0; auto first = studentSet[pool[idx].grade][pool[idx].gender].begin(); return pool[*first].id; } int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][7], int mScore) { set<int, CMP> temp; pool[0].score = mScore; for(int i = 0; i < mGradeCnt; i++){ for(int j = 0; j < mGenderCnt; j++){ int gender = strcmp(mGender[j], MALE) == 0 ? 0 : 1; auto it = studentSet[mGrade[i]-1][gender].lower_bound(0); if(it == studentSet[mGrade[i]-1][gender].end()) continue; temp.insert(*it); } } if(temp.size() == 0) return 0; return pool[*temp.begin()].id; } ////////////////// using namespace std; #define MAX_S 1000005 #define MAX_POST 110005 #include<unordered_map> #include<algorithm> #include<string> #include<vector> unordered_map<string, vector<int>> pool; vector<vector<int>*> postList[MAX_POST]; int idx[MAX_S], id; //id la so luong post, idx la mang luu id cua void addPosting(int mID, int mHashTagNum, char mHashTags[][10]) { int ID = id++; idx[mID] = ID; postList[ID].clear(); if(mHashTagNum == 1){ pool[mHashTags[0]].emplace_back(mID); postList[ID].emplace_back(&pool[mHashTags[0]]); }else{ for (int i = 0; i < mHashTagNum; i++) { string nameT = mHashTags[i]; pool[nameT].emplace_back(mID); postList[ID].emplace_back(&pool[nameT]); for (int j = i+1; j < mHashTagNum; j++) { string nameT2 = mHashTags[j]; if(nameT < nameT2){ nameT2 = nameT+nameT2; } else{ nameT2 = nameT2+nameT; } pool[nameT2].emplace_back(mID); postList[ID].emplace_back(&pool[nameT2]); } } } } void init(int N, int mPostingIDs[], int mHashTagNum[], char mHashTags[][5][10]) { pool.clear(); id = 0; for (int i = N-1; i >= 0; i--) { addPosting(mPostingIDs[i], mHashTagNum[i], mHashTags[i]); } } void removePosting(int mID) { int ID = idx[mID]; for(auto list: postList[ID]){ for (int i = 0;; i++) { if((*list)[i] == mID) { list->erase(list->begin()+i); break; } } } } int findPostingList(int M, char mKeyWords[][10], int mPageNum, int mAnsIDs[]) { string key = mKeyWords[0]; if( M == 2 ){ string key2 = mKeyWords[1]; if(key < key2){ key = key+key2; } else{ key = key2+key; } } auto& list = pool[key]; int p = (mPageNum - 1)*10; int si = list.size(); if(p>=si) return 0; int numofPost_Page = min(p+10, si); int res = 0; for (int i = p; i < numofPost_Page; i++, res++) { mAnsIDs[res] = list[si-1-i]; } return res; }
Leave a Comment