using System; using System.Collections.Generic; class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("ABC"); //5 //文字列"ABC"の隣り合う2つの文字を何度か入れ替えてできる文字列は、以下の6通りです。 //ABC ACB //BAC BCA //CAB CBA //ただし、入力文字列自体は含まないので、"ABC"を除いた5種類を作ることができます。 } else if (InputPattern == "Input2") { WillReturn.Add("DDDDDD"); //0 //何度入れ替えても、"DDDDDD"以外に作ることはできません。 //入力文字列自体は含まないので、"DDDDDD"を除いた0種類が答えです。 } else if (InputPattern == "Input3") { WillReturn.Add("XYZYX"); //29 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); string S = InputList[0]; var stk = new Stack(); stk.Push(S); var VisitedSet = new HashSet() { S }; while (stk.Count > 0) { string Popped = stk.Pop(); for (int I = 0; I <= S.Length - 2; I++) { char[] NewCharArr = Popped.ToCharArray(); NewCharArr[I] = Popped[I + 1]; NewCharArr[I + 1] = Popped[I]; string NewStr = new string(NewCharArr); if (VisitedSet.Add(NewStr)) stk.Push(NewStr); } } Console.WriteLine(VisitedSet.Count - 1); } }