Google Code Jam : Sprint to Google I/O 2012
GeekBearで教えてもらったGoogle Code Jam : Sprint to Google I/O 2012をやってみた。
しかし、提出には至らず。
Cでも書いてみたけど、めんどくさい。
特にProblem-Bは、他の高級言語でやるのに比べて難易度が上がりすぎる気がする。
その後にC++で書き直してみると、C++とSTLの偉大さが良く分かる。
今回みたいにコーディングスピードが求められる場合、Cはどう考えても向かないんだけど、Cでチマチマ書くのが楽しいのです。
Pythonだとすごく楽に書けるそうな。
冗長なコードになったので、そのうち書き直したい。
Cで書いたコードはカオスすぎるので載せません。
Problem A. Android Figurines
namespace Sprint.to.Google.IO._2012.A { public static class Program { public static void Main(string[] args) { var sb = new System.Text.StringBuilder(); using (var sr = new System.IO.StreamReader(args[0], System.Text.Encoding.ASCII)) { sr.ReadLine(); for (int i = 1; !sr.EndOfStream; ++i) { string[] a = sr.ReadLine().Split(' '); int l = int.Parse(a[0]); int m = int.Parse(a[1]); int t; int max = 0; for (int j = 2; j < a.Length; ++j) { t = int.Parse(a[j]); if (max < t) max = t; } sb.Append(string.Format("Case #{0}: ", i)); if (l < max) sb.AppendLine("-1"); else sb.AppendLine((m * l - (l - max)).ToString()); } } System.IO.File.WriteAllText("A-answer.out", sb.ToString(), System.Text.Encoding.ASCII); } } }
#include <fstream> #include <sstream> #include <string> int main(int argc, char** argv) { int l, m, t, c, max; std::ifstream ifs(argv[1]); std::ofstream ofs("A-answer.out"); std::stringstream ss; std::string s; ifs >> c; for (int i = 1; i <= c; ++i) { max = 0; ifs >> l >> m; std::getline(ifs, s); ss << s; while (!ss.eof()) { ss >> t; if (max < t) max = t; } ss.clear(); ofs << "Case #" << i << ": "; if (l < max) ofs << "-1"; else ofs << m * l - (l - max); ofs << std::endl; } return 0; }
Problem B. Repeated Numbers
namespace Sprint.to.Google.IO._2012.B { public static class Program { public static void Main(string[] args) { var sb = new System.Text.StringBuilder(); var l = new System.Collections.Generic.List<int>(); using (var sr = new System.IO.StreamReader(args[0], System.Text.Encoding.ASCII)) { sr.ReadLine(); for (int i = 1; !sr.EndOfStream; ++i) { string[] a = sr.ReadLine().Split(' '); int k = int.Parse(a[0]); for (int j = 0; j < a[1].Length && k < a[1].Length - j; ++j) { string t = a[1].Substring(j, k); if (a[1].Substring(a[1].IndexOf(t) + 1).IndexOf(t) != -1) { int n = int.Parse(t); if (l.IndexOf(n) == -1) l.Add(n); } } sb.Append(string.Format("Case #{0}:", i)); if (l.Count == 0) sb.Append(" NONE"); else { l.Sort(); foreach (int it in l) sb.Append(string.Format(" {0}", it)); l.Clear(); } sb.AppendLine(); } } System.IO.File.WriteAllText("B-answer.out", sb.ToString(), System.Text.Encoding.ASCII); } } }
#include <algorithm> #include <vector> #include <fstream> #include <string> #include <cstring> #include <cstdlib> int main(int argc, char** argv) { std::ifstream ifs(argv[1]); std::ofstream ofs("B-answer.out"); std::string s, t; std::vector<int> v; int k, n, c; ifs >> c; for (int i = 1; i <= c; ++i) { ifs >> k >> s; for (int j = 0, l = s.length(); j < l && k < l - j; ++j) { t = s.substr(j, k); if (strstr(strstr(s.c_str(), t.c_str()) + 1, t.c_str())) { n = strtol(t.c_str(), NULL, 10); bool exist = false; for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) { if (*it == n) { exist = true; break; } } if (!exist) v.push_back(n); } } ofs << "Case #" << i << ':'; if (!v.size()) ofs << " NONE"; else { std::sort(v.begin(), v.end()); for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) ofs << ' ' << *it; v.clear(); } ofs << std::endl; } return 0; }
なんか突っ込みどころがあればこっそりと優しく教えて下さい。