Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
3
Indexable
Never
#include <unordered_map>
#include <string>
#include <vector>

using namespace std;

#define MAX_N			5
#define SIZE			50009
#define MAX_NAME_LEN	7
#define MAX_TAG_LEN		4

//void mstrcpy(char dst[], const char src[]) {
//	int c = 0;
//	while ((dst[c] = src[c]) != '\0') ++c;
//}

//int mstrcmp(const char str1[], const char str2[]) {
//	int c = 0;
//	while (str1[c] != '\0' && str1[c] == str2[c]) ++c;
//	return str1[c] - str2[c];
//}

struct Book{
	 int section;
	 bool isDel;
};

unordered_map<string, int> bookMap;
unordered_map<string, vector<int>> typeMap;
int idx, flag;
Book data[SIZE];
int bookCount[SIZE];

void init(int M)
{
	flag = 0;
	idx = 0;
	for(int i = 0; i < SIZE; i++){
		bookCount[i] = 0;
	}
	bookMap.clear();
	typeMap.clear();
}

void add(char mName[MAX_NAME_LEN], int mTypeNum, char mTypes[MAX_N][MAX_TAG_LEN], int mSection)
{
	bookMap[mName] = idx;
	data[idx].section = mSection;
	data[idx].isDel = false;
	for(int i = 0; i < mTypeNum; i++){
		typeMap[mTypes[i]].push_back(idx);
	}
	idx++;
}

int moveType(char mType[MAX_TAG_LEN], int mFrom, int mTo)
{
	int cnt = 0;
	for(auto it : typeMap[mType]){
		if(data[it].section == mFrom && data[it].isDel == false){
			data[it].section = mTo;
			cnt++;
		}
	}
	return cnt;
}

void moveName(char mName[MAX_NAME_LEN], int mSection)
{
	data[bookMap[mName]].section = mSection;
}

void deleteName(char mName[MAX_NAME_LEN])
{
	data[bookMap[mName]].isDel = true;
}

int countBook(int mTypeNum, char mTypes[MAX_N][MAX_TAG_LEN], int mSection)
{
	flag = flag + 1;
	int cnt = 0;
	for(int i = 0; i < mTypeNum; i++){
		for(auto it : typeMap[mTypes[i]]){
			if(data[it].section == mSection && data[it].isDel == false && bookCount[it] != flag){
				bookCount[it] = flag;
				cnt++;
			}
		}
	}
	return cnt;
}