結果
| 問題 |
No.22 括弧の対応
|
| コンテスト | |
| ユーザー |
aketijyuuzou
|
| 提出日時 | 2024-10-10 21:35:01 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 31 ms / 5,000 ms |
| コード長 | 1,791 bytes |
| コンパイル時間 | 1,060 ms |
| コンパイル使用メモリ | 115,052 KB |
| 実行使用メモリ | 27,120 KB |
| 最終ジャッジ日時 | 2024-10-10 21:35:04 |
| 合計ジャッジ時間 | 2,651 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / 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);
}
}
}
}
aketijyuuzou