#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
struct P {
string name;
int sex; // 1 F 0 M
int year;
int month;
int day;
} people[500];
map<string, int> mapByName;
map<int, int> mapBySex;
map<int, int> mapByYear;
map<int, int> mapByMonth;
map<int, int> mapByDay;
vector<int> vecPool[1000];
int vecPoolIdx = 0;
void addToMaps(P p, int idx) {
if (mapByName.find(p.name) == mapByName.end()) mapByName[p.name] = vecPoolIdx++;
vecPool[mapByName[p.name]].push_back(idx);
if (mapBySex.find(p.sex) == mapBySex.end()) mapBySex[p.sex] = vecPoolIdx++;
vecPool[mapBySex[p.sex]].push_back(idx);
if (mapByYear.find(p.year) == mapByYear.end()) mapByYear[p.year] = vecPoolIdx++;
vecPool[mapByYear[p.year]].push_back(idx);
if (mapByMonth.find(p.month) == mapByMonth.end()) mapByMonth[p.month] = vecPoolIdx++;
vecPool[mapByMonth[p.month]].push_back(idx);
if (mapByDay.find(p.day) == mapByDay.end()) mapByDay[p.day] = vecPoolIdx++;
vecPool[mapByDay[p.day]].push_back(idx);
}
void init() {
for (int i = 0; i < vecPoolIdx; ++i) vecPool[i].clear();
vecPoolIdx = 0;
mapByName.clear();
mapBySex.clear();
mapByYear.clear();
mapByMonth.clear();
mapByDay.clear();
}
vector<int> getRes(int vecName, int vecSex, int vecYear, int vecMonth, int vecDay) {
vector<int> res;
map<int,int> M;
int all = 0;
if (vecName != -1) {
for (int i = 0; i < vecPool[vecName].size(); ++i) {
int now = vecPool[vecName][i];
if (M.find(now) == M.end()) M[now] = 1;
else ++M[now];
}
} else ++all;
if (vecSex != -1) {
for (int i = 0; i < vecPool[vecSex].size(); ++i) {
int now = vecPool[vecSex][i];
if (M.find(now) == M.end()) M[now] = 1;
else ++M[now];
}
} else ++all;
if (vecYear != -1) {
for (int i = 0; i < vecPool[vecYear].size(); ++i) {
int now = vecPool[vecYear][i];
if (M.find(now) == M.end()) M[now] = 1;
else ++M[now];
}
} else ++all;
if (vecMonth != -1) {
for (int i = 0; i < vecPool[vecMonth].size(); ++i) {
int now = vecPool[vecMonth][i];
if (M.find(now) == M.end()) M[now] = 1;
else ++M[now];
}
} else ++all;
if (vecDay != -1) {
for (int i = 0; i < vecPool[vecDay].size(); ++i) {
int now = vecPool[vecDay][i];
if (M.find(now) == M.end()) M[now] = 1;
else ++M[now];
}
} else ++all;
for (map<int, int>::iterator itor = M.begin(); itor != M.end(); ++itor) {
if (itor->second + all == 5) res.push_back(itor->first);
}
return res;
}
int main(int argc, char** argv) {
int T;
scanf("%d", &T);
while (T--) {
init();
int N, M;
scanf("%d%d", &N, &M);
char tmp[100];
for (int i = 0; i < N; ++i) {
scanf("%s", tmp);
people[i].name = tmp;
scanf("%s", tmp);
string sex = tmp;
people[i].sex = sex == "Male" ? 0 : 1;
scanf("%s", tmp);
string birth = tmp;
people[i].year = atoi(birth.substr(0, 4).c_str());
people[i].month = atoi(birth.substr(5, 2).c_str());
people[i].day = atoi(birth.substr(8, 2).c_str());
addToMaps(people[i], i);
}
getchar();
while (M--) {
string query;
getline(cin, query);
string name;
int sex, year, month, day;
int nameIdx = query.find("Name", 0);
if (nameIdx != string::npos) {
int first = query.find('\'', nameIdx + 1);
int second = query.find('\'', first + 1);
int nameLen = second - first - 1;
name = query.substr(first + 1, nameLen);
} else name = "";
int sexIdx = query.find("Sex", 0);
if (sexIdx != string::npos) {
int first = query.find('\'', sexIdx + 1);
int second = query.find('\'', first + 1);
int sexLen = second - first - 1;
sex = sexLen == 6 ? 1 : 0;
} else sex = -1;
int birthIdx = query.find("Birthday", 0);
if (birthIdx != string::npos) {
int first = query.find('\'', birthIdx + 1);
int second = query.find('\'', birthIdx + 1);
int dateLen = second - first - 1;
string date = query.substr(first + 1, dateLen);
first = date.find('/', 0);
second = date.find('/', first + 1);
if (date[0] == '*') year = -1;
else year = atoi(date.substr(0, 4).c_str());
if (date[first + 1] == '*') month = -1;
else month = atoi(date.substr(first + 1, 2).c_str());
if (date[second + 1] == '*') day = -1;
else day = atoi(date.substr(second + 1, 2).c_str());
} else year = month = day = -1;
int vecName, vecSex, vecYear, vecMonth, vecDay;
if (mapByName.find(name) != mapByName.end()) vecName = mapByName[name];
else vecName = -1;
if (mapBySex.find(sex) != mapBySex.end()) vecSex = mapBySex[sex];
else vecSex = -1;
if (mapByYear.find(year) != mapByYear.end()) vecYear = mapByYear[year];
else vecYear = -1;
if (mapByMonth.find(month) != mapByMonth.end()) vecMonth = mapByMonth[month];
else vecMonth = -1;
if (mapByDay.find(day) != mapByDay.end()) vecDay = mapByDay[day];
else vecDay = -1;
vector<int> res = getRes(vecName, vecSex, vecYear, vecMonth, vecDay);
if (res.size() == 0) {
printf("NULL\n");
continue;
}
for (int i = 0; i < res.size(); ++i) printf("%s\n", people[res[i]].name.c_str());
}
}
return 0;
}
291. 数据库检索-软件14
最新推荐文章于 2022-06-30 20:00:23 发布