AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC268-D Unique Username
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("1 1");
WillReturn.Add("chokudai");
WillReturn.Add("chokudai");
//-1
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 2");
WillReturn.Add("choku");
WillReturn.Add("dai");
WillReturn.Add("chokudai");
WillReturn.Add("choku_dai");
//dai_choku
}
else if (InputPattern == "Input3") {
WillReturn.Add("2 2");
WillReturn.Add("chokudai");
WillReturn.Add("atcoder");
WillReturn.Add("chokudai_atcoder");
WillReturn.Add("atcoder_chokudai");
//-1
}
else if (InputPattern == "Input4") {
WillReturn.Add("4 4");
WillReturn.Add("ab");
WillReturn.Add("cd");
WillReturn.Add("ef");
WillReturn.Add("gh");
WillReturn.Add("hoge");
WillReturn.Add("fuga");
WillReturn.Add("____");
WillReturn.Add("_ab_cd_ef_gh_");
//ab__ef___cd_gh
}
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 N = wkArr[0];
int M = wkArr[1];
var SList = new List<string>();
foreach (string EachStr in InputList.Skip(1).Take(N)) {
SList.Add(EachStr);
}
// 可能な_の最大数
var SpaceList = new List<string>();
int SpaceLimit = 16 - SList.Sum(pX => pX.Length);
if (SList.Count >= 3) {
SpaceLimit -= SList.Count - 2;
}
for (int I = 1; I <= SpaceLimit; I++) {
SpaceList.Add(new string('_', I));
}
var TSet = new HashSet<string>();
foreach (string EachStr in InputList.Skip(1 + N)) {
TSet.Add(EachStr);
}
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.SelectedIndList = new List<int>();
WillPush.CurrStr = "";
Stk.Push(WillPush);
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
if (Popped.CurrStr.Length > 16) continue;
if (Popped.SelectedIndList.Count == SList.Count) {
if (Popped.CurrStr.Length >= 3) {
if (TSet.Contains(Popped.CurrStr) == false) {
Console.WriteLine(Popped.CurrStr);
return;
}
}
continue;
}
for (int I = 0; I <= SList.Count - 1; I++) {
if (Popped.SelectedIndList.Contains(I)) continue;
WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList);
WillPush.SelectedIndList.Add(I);
if (WillPush.SelectedIndList.Count == 1) {
WillPush.CurrStr = SList[I];
Stk.Push(WillPush);
}
else {
foreach (string EachSpace in SpaceList) {
WillPush.CurrStr = Popped.CurrStr + EachSpace + SList[I];
int RestLen = 16 - WillPush.CurrStr.Length;
int NeedLen = 0;
for (int J = 0; J <= SList.Count - 1; J++) {
if (WillPush.SelectedIndList.Contains(J)) continue;
NeedLen += SList[J].Length;
NeedLen++; // 文字列の間の分
}
NeedLen--; // 文字列の間を余計に計上してるので、1引く
// 枝切り
if (NeedLen > RestLen) break;
Stk.Push(WillPush);
}
}
}
}
Console.WriteLine(-1);
}
}
struct JyoutaiDef
{
internal List<int> SelectedIndList;
internal string CurrStr;
}
解説
DFSで解候補を列挙してます。