原题链接
Problem Description
Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That’s because this magical paper doesn’t allow character number i in the English alphabet to be written on it in a string of length more than ai. For example, if a1 = 2 he can’t write character ‘a’ on this paper in a string of length 3 or more. String “aa” is allowed while string “aaa” is not.
Mahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be n and they shouldn’t overlap. For example, if a1 = 2 and he wants to send string “aaa”, he can split it into “a” and “aa” and use 2 magical papers, or into “a”, “a” and “a” and use 3 magical papers. He can’t split it into “aa” and “aa” because the sum of their lengths is greater than n. He can split the message into single string if it fulfills the conditions.
A substring of string s is a string that consists of some consecutive characters from string s, strings “ab”, “abc” and “b” are substrings of string “abc”, while strings “acb” and “ac” are not. Any string is a substring of itself.
While Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions:
How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is n and they don’t overlap? Compute the answer modulo 109 + 7.
What is the maximum length of a substring that can appear in some valid splitting?
What is the minimum number of substrings the message can be spit in?
Two ways are considered different, if the sets of split positions differ. For example, splitting “aa|a” and “a|aa” are considered different splittings of message “aaa”.
Input
The first line contains an integer n (1 ≤ n ≤ 103) denoting the length of the message.
The second line contains the message s of length n that consists of lowercase English letters.
The third line contains 26 integers a1, a2, …, a26 (1 ≤ ax ≤ 103) — the maximum lengths of substring each letter can appear in.
Output
Print three lines.
In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 10^9 + 7.
In the second line print the length of the longest substring over all the ways.
In the third line print the minimum number of substrings over all the ways.
Sample Input
10
abcdeabcde
5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output
401
4
3
题目大意
题中给定一个字符串(均由小写字母构成),每个小写字母对应一个数字,表明这个字符可以存在的字符串的最长长度。要求你对这个字符串进行分割,问1)有多少种分割的方法。 2)分割出的字符串的最长长度 。3)分割的字符串的最少数目。
解题思路
遍历每一个字符,考虑第i个字符,limit为当前字符串可支持的最长长度,使j从i->1逆序遍历,j每移动到一个新位置就更新一下limit的值,直到i-j+1的长度超出limit,此时跳出循环。在循环内部时
dp1[i]=(dp1[i]+dp1[j-1])%MOD;
dp2[i]=max(dp2[i],max(i-j+1,dp2[j-1]));
dp3[i]=min(dp3[i],dp3[j-1]+1);
dp1:自己组成一段,方案数就是+= 前i-1的方案数,如果与前面一个字符组成一段,就是+=前i-2,以此类推。
dp2:j每次移动后,比较当前的字符串长度和dp2[j-1]的值。
dp3:dp3[j-1]加上当前字符串与dp3[i]进行比较。
由于涉及min,max操作,要注意数组的预设。
AC代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define PI 3.1415927
#define inf 0x3f3f3f3f
#define fi first
#define se second
using namespace std;
const int MOD=1e9+7;
int main(void)
{
string in;
int n;
int a[26];
int dp1[1005];
int dp2[1005];
int dp3[1005];
cl(dp1);
cl(dp2);
fil(dp3,inf);
cin>>n;
cin>>in;
in=' '+in;
rep(i,0,26) scanf("%d",&a[i]);
dp1[0]=1;
dp3[0]=0;
for(int i=1;i<=n;++i)
{
int limit=a[in[i]-'a'];
for(int j=i;j>=1;--j)
{
limit=min(limit,a[in[j]-'a']);
if(limit<i-j+1)
{
break;
}
else
{
dp1[i]=(dp1[i]+dp1[j-1])%MOD;
dp2[i]=max(dp2[i],max(i-j+1,dp2[j-1]));
dp3[i]=min(dp3[i],dp3[j-1]+1);
}
}
}
cout<<dp1[n]<<endl<<dp2[n]<<endl<<dp3[n]<<endl;
return 0;
}