766C - Mahmoud and a Message(dp)

https://codeforces.com/problemset/problem/766/C


有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母不能出现在长度超过该值的子串中。

求有多少种划分该字符串的方法
求该字符串划分成子串后最大的子串的长度
求该字符串划分成满足要求的子串需要至少划分多少次


思路:dp[i]定义到i的答案,枚举最后一段的长度用以划分

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+100;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
LL limit[30];
LL dp[maxn],maxlen=0,pd[maxn];
LL sum[maxn][30];
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   cin>>(str+1);
   for(LL i=1;i<=26;i++) cin>>limit[i];
   memset(pd,0x3f,sizeof(pd));
   dp[0]=1;
   pd[0]=0;
   for(LL i=1;i<=n;i++){
       LL mx=limit[str[i]-'a'+1];
       for(LL j=i;j>=1;j--){
            LL len=i-j+1;
            mx=min(mx,limit[(str[j]-'a'+1)]);
            if(len>mx) break;
            dp[i]=(dp[i]%mod+dp[j-1]%mod)%mod;
            maxlen=max(maxlen,len);
            pd[i]=min(pd[i],pd[j-1]+1);
              
       }
   }
   cout<<dp[n]<<"\n"<<maxlen<<"\n"<<pd[n]<<"\n";
   return 0;
}
/*有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母不能出现在长度超过该值的子串中。///

求有多少种划分该字符串的方法
求该字符串划分成子串后最大的子串的长度
求该字符串划分成满足要求的子串需要至少划分多少次*/

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/115339951