結果

問題 No.3143 Colorless Green Parentheses Sleep Furiously
ユーザー pengin_2000
提出日時 2025-05-16 22:27:52
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,116 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 27,084 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-17 00:32:05
合計ジャッジ時間 2,626 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:7:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    7 |         scanf("%d %d", &n, &k);
      |         ^~~~~~~~~~~~~~~~~~~~~~
main.c:8:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 |         scanf("%s", s);
      |         ^~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
char s[100005];
int cnt[100005];
int main()
{
	int n, k;
	scanf("%d %d", &n, &k);
	scanf("%s", s);
	int i;
	int val, depth;
	val = 0;
	for (i = 0; i < n; i++)
	{
		if (s[i] == '(')
			val++;
		else
			val--;
		if (val < 0)
		{
			printf("No\n");
			return 0;
		}
	}
	if (val > 0)
	{
		printf("No\n");
		return 0;
	}
	val = depth = 0;
	cnt[0] = 0;
	for (i = 0; i < n; i++)
	{
		if (s[i] == '(')
		{
			depth++;
			cnt[depth] = 0;
		}
		else
		{
			if (cnt[depth] == 0)
				val += 2;
			else if (cnt[depth] == 1)
				val += 1;
			depth--;
			cnt[depth]++;
		}
	}
	if (cnt[0] == 1)
		val++;
	if (val > k)
	{
		printf("No\n");
		return 0;
	}
	printf("Yes\n");
	cnt[0] = 0;
	val = depth = 0;
	for (i = 0; i < n; i++)
	{
		if (s[i] == '(')
		{
			depth++;
			cnt[depth] = 0;
			printf("(");
		}
		else
		{
			if (cnt[depth] == 0)
			{
				printf("1+1");
				val += 2;
			}
			else if (cnt[depth] == 1)
			{
				printf("+1");
				val++;
			}
			printf(")");
			if (s[i + 1] == '(')
				printf("+");
			depth--;
			cnt[depth]++;
		}
	}
	for (i = val; i < k; i++)
		printf("+1");
	printf("\n");
	return 0;
}
0