# HackerCup ## 1 ```cpp // HackerCup1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 #include <iostream> #include <string> int main() { int testCase = 0; std::cin >> testCase; for (auto i = 0; i < testCase; ++i) { std::string input; std::cin >> input; int Bcount = 0; for (const auto& v : input) { if (v == 'B') { Bcount++; } } auto result = 'Y'; if (input.size() <= 2 || Bcount < (input.size() / 2) || Bcount == input.size() - 1) { result = 'N'; } std::cout << "Case #" << std::to_string(i+1) << ": " << result << std::endl; } } ``` ## 2 ```cpp // HackerCup2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 #include <iostream> #include <string> int main() { int testCase = 0; std::cin >> testCase; for (auto i = 0; i < testCase; ++i) { std::string input; std::cin >> input; int Bcount = 0; for (const auto& v : input) { if (v == 'B') { Bcount++; } } auto result = 'Y'; if (input.size() <= 2 || Bcount < 2 || Bcount == input.size() - 1) { result = 'N'; } std::cout << "Case #" << std::to_string(i + 1) << ": " << result << std::endl; } } ``` ## 3 (x|(X&(0|(1&0)))) x| X & 0|(1&0) x| X & 0| 0 1 23 3 3 3210 (0|(1&((1|0)|(1&0)))) ((0|(1&((1|0)|(1&0))))|0) ```cpp #include <iostream> #include <string> #include <algorithm> static const uint64_t MOD = 1000000007; uint64_t getPower(int unsigned y) { uint64_t res = 1; // Initialize result uint64_t x = 2; auto p = MOD; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res; } int solution() { using namespace std; int size, K; cin >> size; cin >> K; string input; cin >> input; uint64_t pay = 0; int cnt = 0; for (auto i = size - 1; i >= 0; --i) { switch (input[i]) { case 'A': cnt = max(0, cnt - 1); break; case 'B': if (cnt == K) { uint64_t price = 1; pay = (pay + getPower(i+1)) % MOD; cnt --; } else { cnt++; } break; } } return pay; } int main() { int testCase = 0; std::cin >> testCase; for (auto i = 0; i < testCase; ++i) { const auto result = solution(); std::cout << "Case #" << i + 1 << ": " << result << std::endl; } return 0; } ``` ## 4 先說一下理解 題目輸入 N 個節點,提出 M 個需求,需求為針對 Node A & B 的 LCA 必須為 C 輸入為 CASE M N A B C A B C... M N A B C A B C... 輸出為: CASE 1 : AP BP CP 如果自己已經是 root 則顯示 0