您的位置:首页 > 其它

Codeforces Round #396(Div. 2)C. Mahmoud and a Message【dp】

2017-02-09 17:03 411 查看
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


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

题目大意:

给你一个长度为N的由小写字母组成的字符串,我们的任务是将这个字符串分成若干子串。

其中ai表示第i个字母不能在大于ai的子串中出现。

问一共有多少种分配方案。

问这些分配方案中,最长子串的长度。

问这些分配方案中,最少分成的子串的个数。

思路:

1、观察到N最大是1e3.估计这是一道n^2或者是n^2logn的题,计数问题,考虑dp:

①设定dp【i】表示处理字符串到位子i有多少种方案。

②那么不难推出其状态转移方程:dp【i】+=dp【i-j】(1<=j<=ai),途中如果遇到了某个字符的子串长度不能参与进来,那么就break掉。

③过程维护取模即可。

2、对于最长子串的长度,我们维护最大枚举到的j即可。

对于最少分成的子串个数,我们可以再设定一个数组:dp2【i】表示处理字符串到位子i最少分成的段数。

那么有:dp2【i】=min(dp2【i】,dp2【i-j】+1);

Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define mod 1000000007
int dp[1050];
int dp2[1050];
char a[106000];
int pos[30];
int main()
{
int n;
while(~scanf("%d",&n))
{
scanf("%s",a+1);
for(int i=0;i<26;i++)scanf("%d",&pos[i]);
memset(dp,0,sizeof(dp));
memset(dp2,0x3f3f3f3f,sizeof(dp2));
dp[0]=1;
dp2[0]=0;
int maxn=0;
for(int i=1;i<=n;i++)
{
int minn=0x3f3f3f3f;
for(int j=1;j<=pos[a[i]-'a'];j++)
{
minn=min(minn,pos[a[i-j+1]-'a']);
if(minn>=j)
{
dp[i]=(dp[i]+dp[i-j])%mod;
dp2[i]=min(dp2[i],dp2[i-j]+1);
maxn=max(maxn,j);
}
else break;
}
}
printf("%d\n",dp
);
printf("%d\n",maxn);
printf("%d\n",dp2
);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces#396Div. 2
相关文章推荐