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".
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.
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 109 + 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.
3
aab
2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3
2
2
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 1
401
4
3
In the first example the three ways to split the message are:
- a|a|b
- aa|b
- a|ab
The longest substrings are "aa" and "ab" of length 2.
The minimum number of substrings is 2 in "a|ab" or "aa|b".
Notice that "aab" is not a possible splitting because the letter 'a' appears in a substring of length 3, while a1 = 2.
题解:我觉得这道题出得很好啊,非常适合新手学DP。
3个DP。
设dp【i】表示前 i 个字母中有多少种合法的方案,则总方案数就是dp【n】。
那么方案转移方程:dp【i】=dp【i】+dp【j】,注意转移方程的条件。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int a[35];
int dp[3][3000];
int main()
{
int n;
cin>>n;
string s;
cin>>s;
for(int i=0;i<26;i++) cin>>a[i];
dp[0][0]=1;
for(int i=1;i<=n;i++){
int len=mod;
dp[1][i]=-mod;
dp[2][i]=mod;
for(int j=i-1;j>=0;--j)
{
len=min(len,a[s[j]-'a']);
if(len>=i-j)
{
//方案数
dp[0][i]=(dp[0][i]+dp[0][j])%mod;
//子串最大长度
dp[1][i]=max(dp[1][i],max(i-j,dp[1][j]) );
//最小子串数
dp[2][i]=min(dp[2][i],dp[2][j]+1);
}
}
}
cout<<dp[0][n]<<endl<<dp[1][n]<<endl<<dp[2][n]<<endl;
return 0;
}