結果
| 問題 |
No.22 括弧の対応
|
| コンテスト | |
| ユーザー |
明智重蔵
|
| 提出日時 | 2015-10-31 18:10:57 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 27 ms / 5,000 ms |
| コード長 | 1,750 bytes |
| コンパイル時間 | 1,991 ms |
| コンパイル使用メモリ | 112,552 KB |
| 実行使用メモリ | 19,328 KB |
| 最終ジャッジ日時 | 2024-07-20 07:04:53 |
| 合計ジャッジ時間 | 3,491 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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("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<string> InputList = GetInputList();
int K = InputList[0].Split(' ').Select(X => int.Parse(X)).Last();
string S = InputList[1];
//開き括弧をスタックにPushし、
//閉じ括弧ならPopすれば、対応が分かる
var stk = new Stack<int>();
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);
}
}
}
}
明智重蔵