結果

問題 No.1845 Long Substrings
ユーザー KKT89KKT89
提出日時 2022-02-18 22:21:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,071 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 136,964 KB
実行使用メモリ 6,736 KB
最終ジャッジ日時 2023-09-11 19:27:26
合計ジャッジ時間 3,758 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
6,620 KB
testcase_01 AC 37 ms
6,736 KB
testcase_02 AC 48 ms
6,516 KB
testcase_03 AC 48 ms
6,680 KB
testcase_04 AC 36 ms
6,632 KB
testcase_05 AC 43 ms
6,092 KB
testcase_06 AC 46 ms
6,620 KB
testcase_07 AC 37 ms
6,616 KB
testcase_08 AC 45 ms
6,344 KB
testcase_09 AC 46 ms
6,236 KB
testcase_10 AC 35 ms
6,512 KB
testcase_11 AC 43 ms
6,084 KB
testcase_12 AC 44 ms
6,304 KB
testcase_13 AC 34 ms
6,588 KB
testcase_14 AC 48 ms
6,328 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

constexpr ll mod = 1e9+7;

// 0-indexed
struct BIT{
    vector<ll> s;
    BIT(int n): s(n){}
    void update(int pos,ll dif){
        for(;pos<s.size();pos|=pos+1)(s[pos]+=dif)%=mod;
    }
    ll query(int pos){ // sum of values in [0,pos)
        ll res=0;
        for(;pos>0;pos&=pos-1)(res+=s[pos-1])%=mod;
        return res;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<ll> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    vector<int> last(26,-1);
    vector<ll> lastdp(26,1);
    string s; cin >> s;
    BIT bit(n);
    for(int i=0;i<n;i++){
        ll u,nx,ad;
        if(last[s[i]-'a'] == -1){
            u = bit.query(i);
            nx = (u+lastdp[s[i]-'a'])%mod;
            ad = nx * a[i] % mod;
            bit.update(i, ad);
            lastdp[s[i]-'a'] = nx;
        }
        else{
            u = bit.query(i) - bit.query(last[s[i]-'a']+1);
            u %= mod;
            if(u<0)u += mod;
            nx = (u+lastdp[s[i]-'a'])%mod;
            ad = nx * a[i] % mod;
            bit.update(i, ad);
            lastdp[s[i]-'a'] = nx;
        }
        last[s[i]-'a'] = i;
        // cout << u << " " << nx << " " << ad << endl;
    }
    cout << bit.query(n) << endl;
}

0