TopCoder SRM 154 DIV 1 Easy 練習
問題
条件にマッチする文字列はどれ?っていう。
解
適当に変換して調べる。
using System; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; public class CheatCode { public int[] matches(string keyPresses, string[] codes) { List<KeyValuePair<char, int>> key = conv(keyPresses); List<int> res = new List<int>(); for (int s = 0; s < codes.Length; ++s) { List<KeyValuePair<char, int>> c = conv(codes[s]); for (int i = 0; i < key.Count - c.Count + 1; ++i) { bool ok = true; for (int j = 0; j < c.Count; ++j) if (!(key[i + j].Key == c[j].Key && key[i + j].Value >= c[j].Value)) { ok = false; break; } if (ok) { res.Add(s); break; } } } return res.ToArray(); } private List<KeyValuePair<char, int>> conv(string s) { List<KeyValuePair<char, int>> res = new List<KeyValuePair<char, int>>(); s += " "; for (int i = 0; i < s.Length - 1; ) { int j = 1; char key = s[i]; while (s[i + j] == key) ++j; i += j; res.Add(new KeyValuePair<char, int>(key, j)); } return res; } }