跳到正文

个人记录

123

jy_ua约 7 分钟
原文

这是 2026年4月25日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年4月11日
更新
2026年5月16日
篇幅
约 7 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
// P10486 [入门赛 #25] sql
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
template <typename K, typename V>
using map = std::unordered_map<K, V>;

// <name, <header, <string, index> > >
map<std::string, map<std::string, map<std::string, std::vector<int>>>> hashs;
map<std::string, std::vector<std::vector<std::string>>> tables;

void CreateTable(std::string name, int row, int col) // It runs correctly. Don't touch it.
{
	// Create Table Array
	std::vector<std::vector<std::string>> table(row + 1, std::vector<std::string>(col + 1));
	for (int i = 1; i <= row; i++)
		for (int j = 1; j <= col; j++)
			std::cin >> table[i][j];
	tables[name] = table;
	// Create Hash For Table
	map<std::string, std::vector<int>> temp;
	map<std::string, map<std::string, std::vector<int>>> temp2;
	for (int i = 1; i <= col; i++)
	{
		std::string colName = table[1][i];
		for (int j = 2; j <= row; j++)
			temp[table[j][i]].push_back(j);
		temp2[colName] = temp;
		temp.clear();
	}
	hashs[name] = temp2;
	return;
}

void Query(std::string tableName, std::vector<std::string> headers, std::string query, std::string value)
{
	// Copy
	map<std::string, map<std::string, std::vector<int>>> hash = hashs[tableName];
	std::vector<std::vector<std::string>> table = tables[tableName];
	// Query
	std::vector<int> idx = hash[query][value];
	// Print
	for (int row : idx)
	{
		for (std::string header : headers)
		{
			for (int col = 1; col <= (int)table[row].size() - 1; col++)
			{
				if (table[1][col] == header) // Find Every Tablehead
				{
					std::cout << table[row][col] << " ";
					break;
				}
			}
		}
		std::cout << '\n';
	}
	return;
}

std::string Get(const std::string &op, char endChar, int &startIdx)
{
	int i = startIdx;
	std::string res;
	while (op[i] != endChar && i < (int)op.length())
	{
		res += op[i];
		i++;
	}
	startIdx = i + 1;
	return res;
}
void Resolve(std::string op, std::string &table, std::vector<std::string> &headers, std::string &query, std::string &value) // It runs well.
{
	// Such As : select sid,name from basic_info where grade=2
	// Also : select headers from table where query=value
	int i = 0;
	i += 7;												// Continue "select" and space to headers
	std::string headersResolve = Get(op, ' ', i) + ','; // GetHeaders
	int j = 0;
	while (j < (int)headersResolve.length())
	{
		std::string header = Get(headersResolve, ',', j);
		if (!header.empty())
			headers.push_back(header);
	}
	i += 5;					 // Continue "from" and space to table
	table = Get(op, ' ', i); // Get table
	i += 6;					 // Continue "where" and space to query
	query = Get(op, '=', i); // Get Query
	value = op.substr(i);	 // Get Value
	while (!value.empty() && (value.back() == ' ' || value.back() == '\n' || value.back() == '\r'))
		value.pop_back();
}

void Solve()
{
	int n, q;
	// Create Tables
	std::cin >> n;
	while (n--)
	{
		std::string str;
		int x, y;
		std::cin >> str >> x >> y;
		CreateTable(str, x, y);
	}
	// Query
	std::cin >> q;
	while (q--)
	{
		std::string str;
		std::getline(std::cin, str);
		if (str.empty())
		{
			q++;
			continue;
		}
		std::string table, query, value;
		std::vector<std::string> headers;
		Resolve(str, table, headers, query, value);
		Query(table, headers, query, value);
	}
}
int main()
{
	Solve();
	return 0;
}