C♯の勉強

C♯4.0 で TopCoder の過去問を解きます。

2013-11-01から1ヶ月間の記事一覧

TopCoder SRM597: LittleElephantAndString

public class LittleElephantAndString { public int getNumber(string A, string B) { if (!A.OrderBy(s => s).SequenceEqual(B.OrderBy(s => s))) return -1; int a = A.Length - 1, b = B.Length - 1; while (a >= 0 && b >= 0) { while (a >= 0 && A[a] …

TopCoder SRM594: AstronomicalRecords

public class AstronomicalRecords { int LCS(long[] A, long[] B) { int n = A.Length; int m = B.Length; var dp = new int[n + 1, m + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (A[i] == B[j]) { dp[i + 1, j + 1] = Math.Ma…

TopCoder SRM594: FoxAndGo2

public class FoxAndGo2 { static public int[] dx = { 0, 1, 0, -1 }; static public int[] dy = { 1, 0, -1, 0 }; public struct Point { public int Row, Col; } public class Group { public List<Point> Points = new List<Point>(); } public class WhiteGroup : Gro</point></point>…

TopCoder SRM597: LittleElephantAndSubset

public class LittleElephantAndSubset { int mod = 1000000007; void enumeratePattern(int[] pattern, int N, int used = 0, long current = 0) { if (current > N) return; if (current > 0) { pattern[used]++; } for (int d = 0; d <= 9; d++) { if (cu…

TopCoder SRM597: LittleElephantAndDouble

それぞれの要素について、2の倍数でなくなるまで2で割って、全ての要素が同じ値になるかどうかを判定する。 2進数文字列で変換 末尾の 0 を除去 全て同じ文字列になるかの判定 で実装してみた。 public class LittleElephantAndDouble { public string ge…

TopCoder SRM596: SparseFactorialDiv2

\(F(n) = n (n - 1^2) (n - 2^2) ... (n - k^2) \) ただし \(k\) は \((n - k^2) > 0\) となる最大の整数とする「\(F(n) \mod m = 0 \)」が成立することと 「\((n - x^2) \mod m = 0\) かつ \( n > x^2 \) となるような自然数 \( x \) が存在すること」は同…

TopCoder SRM596: IncrementAndDoubling

(最長bitの高さ - 1) + 立っている bitの個数が答えになる。いったん2進数文字列に変換すると楽になった。 public class IncrementAndDoubling { public int getMin(int[] desiredArray) { var a = desiredArray.Select(d => Convert.ToString(d, 2)); retu…

TopCoder SRM596: ColorfulRoad

dp[現在の位置] = 最小ステップ数として動的計画法を適用する public class ColorfulRoad { public int getMin(string road) { int n = road.Length; int inf = 1 << 30; var dp = Enumerable.Repeat(inf, n).ToArray(); dp[0] = 0; for (int i = 0; i < n; …

TopCoder SRM596: FoxAndSightseeing

先頭と末尾以外の要素についてスキップを試す。 public class FoxAndSightseeing { public int getMin(int[] position) { int n = position.Length; var candidate = Enumerable.Range(1, n - 2) .Select(i => position.Where((v, index) => index != i)) .T…