Loading

NOIP 模拟 $86\; \rm shiki$

题解 \(by\;zj\varphi\)

每次对于当前的字符,枚举字符集。

显然当前状态一定由当前枚举的字符上一次出现的位置转移而来的,最后用 hashmap 映射一下当前两个字符所对应的所有字符串的奇异值之和。

复杂度为 \(\mathcal{O\rm(26*n)}\)\(\mathcal{O\rm(26*nlogn)}\)

Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    #define debug1(x) std::cerr << #x"=" << x << ' '
    #define debug2(x) std::cerr << #x"=" << x << std::endl
    #define Debug(x) assert(x)
    struct nanfeng_stream{
        template<typename T>inline nanfeng_stream &operator>>(T &x) {
            bool f=false;x=0;char ch=gc();
            while(!isdigit(ch)) f|=ch=='-',ch=gc();
            while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
            return x=f?-x:x,*this;
        }
    }cin;
}
using IO::cin;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    using ll=long long;
    static const int N=1e5+7;
    int lst[30],n,m,k;
    char s[N],t[10];
    ll dp[N],ans;
    std::map<std::string,ll> mp;
    auto ch=[](char s) {return s-'a';};
    inline int main() {
        FI=freopen("shiki.in","r",stdin);
        FO=freopen("shiki.out","w",stdout);
        scanf("%d%s%d",&n,s+1,&m);
        for (ri i(1);i<=m;pd(i)) {
            scanf("%s%s%d",t+1,t+2,&k);
            mp[std::string(t+1)]+=k; 
        }
        lst[ch(s[1])]=1;
        for (ri i(2);i<=n;pd(i)) {
            t[2]=s[i];
            for (ri j(0);j<26;pd(j)) {
                if (!lst[j]) continue;
                t[1]=j+'a';
                dp[i]=cmax(dp[i],dp[lst[j]]+mp[std::string(t+1)]);
            }
            lst[ch(s[i])]=i;
            ans=cmax(ans,dp[i]);
        }
        printf("%lld\n",ans);
        return 0;
    }
}
int main() {return nanfeng::main();}
posted @ 2021-10-31 10:02  ナンカエデ  阅读(41)  评论(0编辑  收藏  举报