結果

問題 No.22 括弧の対応
ユーザー Bantako
提出日時 2017-06-28 19:00:42
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 578 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 22,656 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-20 07:24:37
合計ジャッジ時間 858 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:3:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
    3 | main(){
      | ^~~~
main.cpp: In function ‘int main()’:
main.cpp:5:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    5 |     scanf("%d %d %s",&N,&K,S);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
char S[10001];
main(){
    int N,K;
    scanf("%d %d %s",&N,&K,S);
    int count = 0;//(:+1 ):-1
    if(S[K-1] == '('){    //'('の場合右側を探索
        for(int i = K-1;i < N;i++){
            count += S[i]=='('?1:-1;
            if(count == 0){
                printf("%d\n",i+1);
                return 0;
            }
        }
   }else{
        for(int i = K-1;i >= 0;i--){
            count += S[i]=='('?1:-1;
            if(count == 0){
                printf("%d\n",i+1);
                return 0;
            }
        }
    }
}


0