using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("4 4"); WillReturn.Add("(())"); //1 //(())の文字の4番目は)である、このとき対応する文字は1番目の(である } else if (InputPattern == "Input2") { WillReturn.Add("12 2"); WillReturn.Add("(((())()())) "); //11 //(((())()()))という文字列の2番目の文字は(である。 //このとき対応する文字は11番目の)である。 } else if (InputPattern == "Input3") { WillReturn.Add("20 5"); WillReturn.Add("((((((()))))))(()())"); //10 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); int K = InputList[0].Split(' ').Select(X => int.Parse(X)).Last(); string S = InputList[1]; //開き括弧をスタックにPushし、 //閉じ括弧ならPopすれば、対応が分かる var stk = new Stack(); for (int I = 0; I <= S.Length - 1; I++) { if (S[I] == '(') stk.Push(I); if (S[I] == ')') { int wkPair = stk.Pop(); if (K - 1 == I) Console.WriteLine(wkPair + 1); if (K - 1 == wkPair) Console.WriteLine(I + 1); } } } }