using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { private const string CWW = "c.*?w.*?w"; static void Main(string[] args) { var target = Console.ReadLine(); var r = Regex.Matches(target, CWW); if (r.Count == 0) { Console.WriteLine("-1"); return; } var min = 101; GetMinValue(r, ref min); // 逆方向からの検索 r = Regex.Matches(target, CWW, RegexOptions.RightToLeft); GetMinValue(r, ref min, RegexOptions.RightToLeft); Console.WriteLine(min); Console.ReadKey(); } private static void GetMinValue(MatchCollection target, ref int min, RegexOptions regexOptions = RegexOptions.None) { foreach (Match match in target) { if (min > match.Length) { min = match.MatcheEx(CWW, regexOptions).Value.Length; } } } } public static class MatchEX { public static Match MatcheEx(this Match match, string pat, RegexOptions regexOptions) { var target = match.Value; if(regexOptions == RegexOptions.None) { target = target.Substring(1); } else if(regexOptions == RegexOptions.RightToLeft) { target = target.Substring(0, target.Length - 1); } var search = Regex.Matches(target, pat); if(search.Count == 0) { return match; } else if (search.Count == 1) { return search[0].MatcheEx(pat, regexOptions); } else { throw new Exception("too match"); } } } }