Google Code Jam Qualification Round 2009: äºé¸ä¼
å»å¹´ã«å¼ãç¶ãä»å¹´ãåå ãã¾ãã. C ã® large ãè½ã¨ããã®ã§, 76 ç¹.
A - Alien Language
æ£è¦è¡¨ç¾ã§ä¸æã§è§£ããããã§ãã, ãããªãã¨ã«ã¯æ°ã¥ããªãç§ã¯, å ¨ãã¿ã¼ã³ãæ¢ç´¢ãã¦æ±ããæ¹æ³ãã¨ãã¾ãã.
åç´ã«å ¨æ¢ç´¢ãã㨠small ã§ããéããªãã®ã§, è¾æ¸ã®ã¯ã¼ãã®å ¨æ¥é èªãè¾æ¸ã«è¿½å ãã¦, æåãããå°æå ã®ç´°å·¥ã§, large ã 49 ç§ã§éãã¾ãã.
å ¨æ¢ç´¢ãªã©ããªãã¦ã, ãã¿ã¼ã³ãç°¡åãªã®ã§, è¾æ¸ã®ã¯ã¼ãããããã«å¯¾ãã¦, ä¸æåã¥ã¤ç §åããã°ããã ãã ã£ãã®ã§ãã.
#include <iostream> #include <vector> int main() { size_t L, D, N; std::cin >> L >> D >> N; std::vector<std::string> dic(D, std::string()); for(size_t i = 0; i < D; ++i) { std::cin >> dic[i]; } for(size_t i = 1; i <= N; ++i) { std::string pattern; std::cin >> pattern; std::vector<std::string> pv; for(size_t index = 0; index < pattern.size();) { if('(' == pattern[index]) { std::string::size_type endparen = pattern.find(")", index); pv.push_back(pattern.substr(index+1, endparen-index-1)); index = endparen+1; } else { pv.push_back(pattern.substr(index,1)); ++index; } } int total = 0; for(size_t d = 0; d < D; ++d) { const std::string& word = dic[d]; bool found = true; for(size_t i = 0; i < word.size(); ++i) { if(pv[i].find(word[i]) == std::string::npos) { found = false; break; } } if(found) { ++total; } } std::cout << "Case #" << i << ": " << total << std::endl; } }
ã¢ã«ãã¡ãããå°æåã«éå®ããã¦ããã®ã§, 'a' ããã¿ã¼ã³ã«ãããªã, 1 ãããç®ãã»ãã, 'b' ããããªã, 2 ãããç®ãã»ããã¨è¨ã風ã«ããããã¿ã¼ã³ã«ãã¦, ç §åããããæ¼ç®ã«ããã¨éã解ãã¾ã. i çªãã®æåãã¿ã¼ã³å p[i] ãããããã¿ã¼ã³ã«ããã¨
int bitpattern[i] = 0; for(size_t k = 0; k < p[i].size(); ++k) { bitpattern[i] |= 1 << (p[i][k]-'a'); }
è¾æ¸å ã®ã¯ã¼ã W ã¨ç §åããã«ã¯, W[i]&bitpattern[i] ã 0 ãªãç §å失æ, 1 ãªã次ã®æåã®ç §åã«é²ã¿ã¾ã.
#include <iostream> #include <vector> int main() { size_t L, D, N; std::cin >> L >> D >> N; std::vector<std::string> dic(D, std::string()); for(size_t i = 0; i < D; ++i) { std::cin >> dic[i]; } for(size_t i = 1; i <= N; ++i) { std::string pattern; std::cin >> pattern; std::vector<unsigned int> pv; for(size_t index = 0; index < pattern.size();) { if('(' == pattern[index]) { std::string::size_type endparen = pattern.find(")", index); std::string p = pattern.substr(index+1, endparen-index-1); unsigned int x = 0; for(size_t k = 0; k < p.size(); ++k) { x |= 1 << (p[k]-'a'); } pv.push_back(x); index = endparen+1; } else { pv.push_back(1 << (pattern[index]-'a')); ++index; } } int total = 0; for(size_t d = 0; d < D; ++d) { const std::string& word = dic[d]; bool found = true; for(size_t i = 0; i < word.size(); ++i) { if(!(pv[i] & (1 << (word[i]-'a')))) { found = false; break; } } if(found) { ++total; } } std::cout << "Case #" << i << ": " << total << std::endl; } }
B - Watersheds
ç§ã«ã¨ã£ã¦ã¯ãããä¸çªç°¡åã§ãã. åé¡èªãã®ãé¢åã§ããã. 'drainage basins' ã£ã¦ãªããã, ã£ã¦æãã§.
å·¦ä¸ã®ãã¹ããé ã«, ãã¹ã¦ã®ãã¹ãèãã¦, æµããæ¹åã決ãã¦ããã¾ã.
次ã«, ãã§ã«ã©ãã«ä»ãããã¦ãããã¹ã¯ã¹ãããããªãã, å·¦ä¸ã®ãã¹ããé ã«, æµããæ¹åããã©ã£ã¦, sink ãæ¢ãã¾ã. ããã¦, sink ããæµããéé ã«è¾¿ã£ã¦, ã©ãã«ä»ããã¾ã. æå¾ã«ã©ãã«ãæ´æ°ãã¾ã.
C - Welcome to Code Jam
æåºãããã®ã¯, å帰ã§è§£ãããã®ã§, æç« ã®ä½ç½®ã¨ "welcome to .." ã®ä½ç½®ã®ãã¢ããã¼ã«ãã¦, ãã®æç¹ã§ã®åæ°ãã¡ã¢ãã¾ãã. æ®å¿µãªãã large ã失æãã¦ãã¾ãã.
ããã§ã¯è¡¨ãç¨ãã¦è§£ãã¦ã¿ã¾ã. T ãæç« , W ã "welcome to code jam" ã¨ãã¾ã.
t ã T ã®é·ã, w ã W ã®é·ãããã¾ã.
C[ i ][ j ] ã W[ i ], T[ j ] ã¾ã§è¦ãã¨ãã®å ´åã®æ°ã¨ãã¾ã.
W[ i ] == T[ j ] ã®æ, C[ i-1 ][ j-1 ] ã ã, å ´åã®æ°ãå¢ãããã¨ã«ãªãã¾ã.
W[ i ] != T[ j ] ã®æã¯, å ´åã®æ°ã¯å¢ããªãã®ã§, C[ i ][ j-1 ] ã¾ã§ã®å ´åã®æ°ã¨åãã«ãªãã¾ã. ããã漸åå¼ã«ããã¨,
C[i][j] = C[i][j-1]+C[i-1][j-1] if W[i] == T[j] C[i][j-1] if W[i] != T[j]
i = 0 ã®ã¨ãã« W[ i ] == T[ j ] ã¨ãªã£ãã, å ´åã®æ°ã 1 å¢ããã®ã§,
C[0][j] = 1, 0 <= j <= t C[i][0] = 0, 0 <= i <= w
ã¨ãã¦ããã¾ã. ããã§ããã°ã©ã ãç°¡åã«ãªãã¾ã.
ãã¨ã¯, O(W*T) ã®æéãããã¦, C[ w ][ t ] ãè¨ç®ãã¾ã. C[ w ][ t ] ãçãã§ã.
#include <cstring> #include <iostream> #include <algorithm> #include <iomanip> int main() { const std::string W = "welcome to code jam"; int N; std::cin >> N; std::string line; getline(std::cin, line); for(int n = 1; n <= N; ++n) { std::string T; getline(std::cin, T); int C[20][501]; memset(C, 0, sizeof(C)); std::fill(&C[0][0], &C[0][501], 1); for(size_t i = 1; i <= W.size(); ++i) { for(size_t j = 1; j <= T.size(); ++j) { C[i][j] = C[i][j-1]; if(W[i-1] == T[j-1]) { C[i][j] += C[i-1][j-1]; } C[i][j] %= 10000; } } std::cout << "Case #" << n << ": " << std::setfill('0') << std::setw(4) << C[W.size()][T.size()] << std::endl; } }
ãããæ¬çªã«ã§ããã°ããã®ã§ãã. ç ãã¨ãæ·±å¤ã¨ãææ¥æ©ãã¨ãããããè¨ã訳ã¯ã§ãããã§ãã.