結果

問題 No.1845 Long Substrings
ユーザー 👑 ygussanyygussany
提出日時 2022-02-18 22:09:25
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 829 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 29,364 KB
実行使用メモリ 5,192 KB
最終ジャッジ日時 2023-09-11 19:16:40
合計ジャッジ時間 3,207 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
5,128 KB
testcase_01 AC 26 ms
5,028 KB
testcase_02 AC 39 ms
5,084 KB
testcase_03 AC 39 ms
5,188 KB
testcase_04 AC 28 ms
5,120 KB
testcase_05 AC 34 ms
4,960 KB
testcase_06 AC 37 ms
5,124 KB
testcase_07 AC 27 ms
5,192 KB
testcase_08 AC 36 ms
5,044 KB
testcase_09 AC 37 ms
5,060 KB
testcase_10 AC 26 ms
5,116 KB
testcase_11 AC 35 ms
5,016 KB
testcase_12 AC 36 ms
5,064 KB
testcase_13 AC 27 ms
5,044 KB
testcase_14 AC 38 ms
5,128 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,736 KB
testcase_28 AC 2 ms
4,400 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 1000000007;

void add_BIT(int N, long long BIT[], int i, int k)
{
	while (i <= N) {
		BIT[i] += k;
		i += (i & -i);
	}
}

long long sum_BIT(long long BIT[], int r)
{
	long long sum = 0;
	while (r > 0) {
		sum += BIT[r];
		r -= (r & -r);
	}
	return sum;
}

int main()
{
	int i, N, A[200001];
	char S[200003];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(A[i]));
	scanf("%s", &(S[1]));
	
	int last[26] = {}, dp[200001];
	long long BIT[200001] = {};
	long long ans = 0;
	for (i = 1, dp[0] = 1; i <= N; i++) {
		dp[i] = (sum_BIT(BIT, i - 1) - sum_BIT(BIT, last[S[i] - 'a']) + dp[last[S[i] - 'a']]) % Mod;
		last[S[i] - 'a'] = i;
		ans += (long long)dp[i] * A[i] % Mod;
		add_BIT(N, BIT, i, (long long)dp[i] * A[i] % Mod);
	}
	printf("%lld\n", ans % Mod);
	fflush(stdout);
	return 0;
}
0