結果

問題 No.1845 Long Substrings
ユーザー KKT89KKT89
提出日時 2022-02-18 22:21:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 2,071 bytes
コンパイル時間 1,809 ms
コンパイル使用メモリ 138,068 KB
最終ジャッジ日時 2025-01-28 00:17:00
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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