版本比较
123
正文
增加 22 行删除 119 行
121456782310111213144516171819202122232425262728293031323334356783738394041424344459101112134748495051525354555657581415166061176364656667686970717273181920212223757677787980818283848586878889909192939495962498991001011021031041051061071081091101111121131141151161171181191201211221231241251262526129272829303132333435132```cpp// P10486 [入门赛 #25] sql#include <iostream>#include <algorithm>#include <vector>#include <unordered_map>template <typename K, typename V>using map = std::unordered_map<K, V>;#define ULL unsigned long long
// <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.bool Solve(){ // 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;} ULL n, x; std::cin >> n >> x;
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) if (x == 1) return (n & (n - 1)) == 0; ULL S = 0, tmp = n; while (tmp) { 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'; S += tmp % x; tmp /= x; } 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; ULL d = x - 1; for (ULL p = 1; p <= n; p <<= 1) if (p >= S && (p - S) % d == 0) return true; return false;}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(); std::ios::sync_with_stdio(false); std::cin.tie(nullptr);
int T; std::cin >> T; while (T--) std::cout << (Solve() ? "Yes\n" : "No\n"); return 0;}```