結果
| 問題 |
No.22 括弧の対応
|
| コンテスト | |
| ユーザー |
shimadandy
|
| 提出日時 | 2016-10-03 17:42:05 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 30 ms / 5,000 ms |
| コード長 | 1,798 bytes |
| コンパイル時間 | 5,087 ms |
| コンパイル使用メモリ | 114,184 KB |
| 実行使用メモリ | 19,456 KB |
| 最終ジャッジ日時 | 2024-07-20 07:16:48 |
| 合計ジャッジ時間 | 2,014 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / 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.Linq;
using System.Collections.Generic;
class Program
{
/// <summary>
/// プログラムのエントリポイント
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
// 括弧の数と調査する括弧のインデックスを取得する
var n = (from tmp in Console.ReadLine().Split(' ')
select int.Parse(tmp)).ToArray();
// 一行分読み込む
var s = Console.ReadLine();
// 開きと閉じの対応をリスト化する
var list = new List<Interaction>();
for (int si = 0; si < s.Length; si++)
{
if (s[si] == '(')
{
list.Add(new Interaction() { OpenIndex = si });
}
else
{
for (int i = list.Count - 1; i >= 0; i--)
{
if (list[i].CloseIndex < 0)
{
list[i].CloseIndex = si;
break;
}
}
}
}
// 結果出力
if (s[n[1] - 1] == '(')
Console.WriteLine(list.Where((intr) => intr.OpenIndex == n[1] - 1).ToArray()[0].CloseIndex + 1);
else
Console.WriteLine(list.Where((intr) => intr.CloseIndex == n[1] - 1).ToArray()[0].OpenIndex + 1);
}
/// <summary>
/// 対応情報クラス
/// </summary>
public class Interaction
{
/// <summary>
/// 開き括弧のインデックス
/// </summary>
public int OpenIndex { get; set; }
/// <summary>
/// 閉じ括弧のインデックス
/// </summary>
public int CloseIndex { get; set; } = -1;
}
}
shimadandy