Yiwen with Sqc 题解(dp)

题目链接

题目思路

\(dp[i]\)表示以\(i\)结尾的所有字符串的答案总和

然后自己再手推一下式子即可得到转移方程

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e5+5,inf=0x3f3f3f3f,mod=998244353;
const double eps=1e-6;
char s[maxn];
int cnt[30],sum[30];
ll dp[maxn];
signed main(){
    int _; scanf("%d",&_);
    while(_--){
        memset(cnt,0,sizeof(cnt));
        memset(sum,0,sizeof(sum));
        scanf("%s",s+1);
        int n=strlen(s+1);
        ll ans=0;
        for(int i=1;i<=n;i++){
            int now=s[i]-'a'+1;
            dp[i]=(dp[i-1]+2*sum[now]+i)%mod;
            sum[now]=(sum[now]+i)%mod;
            ans=(ans+dp[i])%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
posted @ 2021-08-10 19:18  hunxuewangzi  阅读(254)  评论(6编辑  收藏  举报