您的位置:首页 > 其它

766C Mahmoud and a Message[字符串][简单dp]

2017-02-10 22:09 316 查看
C. Mahmoud and a Message

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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 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.

Examples

input
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


output
3
2
2


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 1


output
401
4
3


题目大意:

给定长度为n的字符串,只含有小写字母。接下来给出26个数字,代表从a到z的26个英文字母在子字符串中出现的次数限制。

输出三个数字,第一个是符合要求的分割子串的方法的数量,第二个是最大的子串的长度,第三个是最小分割子串数。

思路:

问题考虑substring,我们已知一个string的子串个数是2^n,计算的方法是dp
= dp[n-1] + dp[n-2] + ...+dp[1]。根据此方法,从字符串的低位从高位遍历,对于每一位,从1到i在遍历,若i-j  到i是符合要求的子串,那么dp[j]便应该累加到dp[i]

即 若 i-j到 i 满足要求:

dp[i] += dp[j];

同时MAX代表最大子串 MAX = max(MAX, j);

用f[i]表示最小分割子串的数量 则f[i] = min(f[i-j]+1, f[i]);

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int n;
const int MAXN=1e3+7;
const int mod=1e9+7;
int limit[30];
long long dp[MAXN];
int f[MAXN];
char s[MAXN];

int check(int i,int j)
{
int l=j-i+1;
for(int k=i;k<=j;++k)
{
if(limit[s[k]-'a']<l)return 0;
}
return 1;
}

int main()
{
int i,j;
scanf("%d",&n);
scanf("%s",s+1);
for(i=0;i<26;++i)scanf("%d",&limit[i]);
int MAX=0;
dp[0]=1;
for(i=1;i<=n;++i)//长度为i
{
f[i]=1e9;
for(j=1;j<=i;++j)//长度
{
if(check(i-j+1,i))//后面的当前序列合法
{
dp[i]=(dp[i]+dp[i-j])%mod;
f[i]=min(f[i],f[i-j]+1);
MAX=max(MAX,j);
}
}
}
cout << dp
<< MAX << f
;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: