Codeforces645E Intellectual Inquiry(dp)

本文介绍了一种使用动态规划解决特定字符串问题的方法,旨在计算一个由前K个小写字母组成的字符串,在追加m个字符后能形成的最多不同子序列个数。通过巧妙的状态定义与转移方程,有效避免了重复计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意
给定一个长度为n的只包含前K个小写字母的字符串,现在要在它后面补上m个前K个小写字母,求能得到的最多的不同的子序列个数(空串也算),答案对109+7取模. 
n,m≤105,K≤26.
题解
我们先考虑怎么求一个给定字符串的不同子序列个数. 
关键是dp时怎么保证不重复计数? 
呃…说实话我只是自己瞎定义了一个状态,然后发现可以通过奇妙的转移避免重复计算,所以我也无法很好地解释思路究竟是怎样的(=.=||). 
设dp[i][j]表示从前i个字符中取出的结尾为字符j的方案数.可以写出转移方程: 
显然若str[i]≠j,那么dp[i][j]=dp[i−1][j]. 
而对str[i]=j的情况,有:
dp[i][j]=∑kdp[i−1][k]+1.


即在前面所有的方案后面都加上一个str[i],还算上一种只取str[i]的方案. 
这样转移就保证了每个dp值表示的所有方案中没有重复的,而在结尾不同的方案后面接上str[i]也不会有重复,从而避免了重复计数.加上空串后最终答案为∑dp[n][i]+1. 
于是维护一个dp值之和就可以O(n)求出给定串的不同子序列个数.当然dp的第一维可以省掉. 
然而接下来的m个字符需要我们自己构造,只要写出上面的dp方程就不难了. 
容易发现在前面的dp值确定的情况下不管怎么构造得到的dp值都是不变的,而改变的只是dp值之和.所以可以贪心地选dp值最小的进行构造. 
注意还有一点是这里的dp值是取模过的,所以不能直接比较大小,可以发现dp值显然是单调递增的,所以可以机智地用下标进行比较. 
复杂度是O(n+m⋅K).
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+5,MAX_K=26,P=1e9+7;
int last[MAX_K],dp[MAX_K];
char str[N];
inline void mod_add(int &a,int b){
    if((a+=b)>=P)a-=P;
}
int main(){
    int n,m,K,sum=0,ans=1;
    scanf("%d%d%s",&m,&K,str);
    n=strlen(str);
    for(int i=0;i<K;++i){
        dp[i]=0;
        last[i]=-1;
    }
    for(int i=0;i<n;++i){
        last[str[i]-='a']=i;
        int tmp=dp[str[i]];
        dp[str[i]]=(sum+1)%P;
        sum=(((sum<<1)+1-tmp)%P+P)%P;
    }
    for(int i=0;i<m;++i){
        int ptr=-1;
        for(int j=0;j<K;++j)
            if(ptr==-1||last[j]<last[ptr])ptr=j;
        int tmp=dp[ptr];
        dp[ptr]=(sum+1)%P;
        sum=(((sum<<1)+1-tmp)%P+P)%P;
        last[ptr]=n+i;
    }
    for(int i=0;i<K;++i)
        mod_add(ans,dp[i]);
    printf("%d\n",ans);
    return 0;
}
/*





*/



### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值