AtCoderのPAST
次のPASTの問題へ
前のPASTの問題へ
第12回PAST G Wildcards
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 4 2");
WillReturn.Add("aabc");
WillReturn.Add("bbaa");
WillReturn.Add("abbc");
WillReturn.Add("cccc");
WillReturn.Add("acba");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("5 4 4");
WillReturn.Add("aabc");
WillReturn.Add("bbaa");
WillReturn.Add("abbc");
WillReturn.Add("cccc");
WillReturn.Add("acba");
//5
}
else if (InputPattern == "Input3") {
WillReturn.Add("5 4 0");
WillReturn.Add("aabc");
WillReturn.Add("bbaa");
WillReturn.Add("abbc");
WillReturn.Add("cccc");
WillReturn.Add("acba");
//1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int L = wkArr[1];
int K = wkArr[2];
string[] SArr = InputList.Skip(1).ToArray();
// K=0 の特殊ケース
if (K == 0) {
Console.WriteLine(SArr.Distinct().GroupBy(pX => pX).Max(pX => pX.Count()));
return;
}
var IndList = new List<int>();
for (int I = 0; I <= SArr[0].Length - 1; I++) {
IndList.Add(I);
}
var AnswerKouho = new List<int>();
foreach (int[] EachIndArr in RubyPatternClass<int>.Combination(IndList, K)) {
var CntDict = new Dictionary<string, int>();
foreach (string EachS in SArr) {
var sb = new System.Text.StringBuilder();
for (int I = 0; I <= EachS.Length - 1; I++) {
if (Array.IndexOf(EachIndArr, I) >= 0) {
continue;
}
sb.Append(EachS[I]);
}
string CurrStr = sb.ToString();
if (CntDict.ContainsKey(CurrStr) == false) {
CntDict[CurrStr] = 0;
}
CntDict[CurrStr]++;
}
AnswerKouho.Add(CntDict.Values.Max());
}
Console.WriteLine(AnswerKouho.Max());
}
}
#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
// 組合せを返す
private struct JyoutaiDef_Combination
{
internal int CurrP;
internal List<int> SelectedIndList;
}
internal static IEnumerable<Type[]> Combination(IEnumerable<Type> pEnum, int pR)
{
if (pR == 0) yield break;
Type[] pArr = pEnum.ToArray();
if (pArr.Length < pR) yield break;
var Stk = new Stack<JyoutaiDef_Combination>();
JyoutaiDef_Combination WillPush;
for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
WillPush.CurrP = I;
WillPush.SelectedIndList = new List<int>() { I };
Stk.Push(WillPush);
}
while (Stk.Count > 0) {
JyoutaiDef_Combination Popped = Stk.Pop();
// クリア判定
if (Popped.SelectedIndList.Count == pR) {
var WillReturn = new List<Type>();
Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
yield return WillReturn.ToArray();
continue;
}
for (int I = pArr.GetUpperBound(0); Popped.CurrP + 1 <= I; I--) {
WillPush.CurrP = I;
WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
Stk.Push(WillPush);
}
}
}
}
#endregion
解説
?にする位置を決めたら、
?の位置を無視して左から繋いだ文字列の、最頻値の件数が解候補になります。
なので、?の位置を全て試してます。